Represents a field for reflection
@example Usage
Gorillib::Model::Field.new(:name => 'problems', type => Integer, :doc => 'Count of problems')
all options passed to the field not recognized by one of its own current fields
@param [to_sym] name Field name @param [receive] type Factory for field values. To accept any object as-is, specify `Object` as the type. @param [Gorillib::Model] model Field’s owner @param [Hash{Symbol => Object}] options Extended attributes @option options [String] doc Description of the field’s purpose @option options [true, false, :public, :protected, :private] :reader Visibility for the reader (`foo`) method; `false` means don’t create one. @option options [true, false, :public, :protected, :private] :writer Visibility for the writer (`foo=`) method; `false` means don’t create one. @option options [true, false, :public, :protected, :private] :receiver Visibility for the receiver (`receive_foo`) method; `false` means don’t create one.
# File lib/gorillib/model/field.rb, line 40 def initialize(model, name, type, options={}) Validate.identifier!(name) type_opts = options.extract!(:blankish, :empty_product, :items, :keys, :of) type_opts[:items] = type_opts.delete(:of) if type_opts.has_key?(:of) # @model = model @name = name.to_sym @type = Gorillib::Factory.factory_for(type, type_opts) default_visabilities = visibilities @visibilities = default_visabilities.merge( options.extract!(*default_visabilities.keys) ) @doc = options.delete(:name){ "#{name} field" } receive!(options) end
# File lib/gorillib/model/field.rb, line 74 def ==(val) super && (val._extra_attributes == self._extra_attributes) && (val.model == self.model) end
@return [String] Human-readable presentation of the field definition
# File lib/gorillib/model/field.rb, line 62 def inspect args = [name.inspect, type.to_s, attributes.reject{|k,v| k =~ /^(name|type)$/}.inspect] "field(#{args.join(",")})" end
# File lib/gorillib/model/field.rb, line 66 def inspect_compact "field(#{name})" end
# File lib/gorillib/model/field.rb, line 70 def to_hash attributes.merge!(@visibility).merge!(@_extra_attributes) end
@return [String] the field name
# File lib/gorillib/model/field.rb, line 57 def to_s name.to_s end
returns the visibility
@example reader is protected, no writer:
Foo.field :granuloxity, :reader => :protected, :writer => false
# File lib/gorillib/model/field.rb, line 91 def visibility(meth_type) Validate.included_in!("method type", meth_type, @visibilities.keys) @visibilities[meth_type] end
# File lib/gorillib/model/field.rb, line 101 def inscribe_methods(model) model.__send__(:define_attribute_reader, self.name, self.type, visibility(:reader)) model.__send__(:define_attribute_writer, self.name, self.type, visibility(:writer)) model.__send__(:define_attribute_tester, self.name, self.type, visibility(:tester)) model.__send__(:define_attribute_receiver, self.name, self.type, visibility(:receiver)) end
Generated with the Darkfish Rdoc Generator 2.