class Gorillib::Model::Field

Represents a field for reflection

@example Usage

Gorillib::Model::Field.new(:name => 'problems', type => Integer, :doc => 'Count of problems')

Attributes

_extra_attributes[R]
Hash

all options passed to the field not recognized by one of its own current fields

model[R]
Gorillib::Model

Model owning this field

name[R]

Note: ‘Gorillib::Model::Field` is assembled in two pieces, so that it can behave as a model itself. Defining `name` here, along with some fudge in initialize, provides enough functionality to bootstrap. The fields are then defined properly at the end of the file.

type[R]

Public Class Methods

new(model, name, type, options={}) click to toggle source

@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_visibilities = visibilities
  @visibilities     = default_visibilities.merge( options.extract!(*default_visibilities.keys).compact )
  @doc              = options.delete(:name){ "#{name} field" }
  receive!(options)
end
receive(hsh) click to toggle source
# File lib/gorillib/model/field.rb, line 78
def self.receive(hsh)
  name  = hsh.fetch(:name)
  type  = hsh.fetch(:type)
  model = hsh.fetch(:model)
  new(model, name, type, hsh)
end

Public Instance Methods

==(val) click to toggle source
Calls superclass method Gorillib::Model#==
# File lib/gorillib/model/field.rb, line 74
def ==(val)
  super && (val._extra_attributes == self._extra_attributes) && (val.model == self.model)
end
inspect() click to toggle source

@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
inspect_compact() click to toggle source
# File lib/gorillib/model/field.rb, line 66
def inspect_compact
  "field(#{name})"
end
to_hash() click to toggle source
# File lib/gorillib/model/field.rb, line 70
def to_hash
  attributes.merge!(@visibility).merge!(@_extra_attributes)
end
to_s() click to toggle source

@return [String] the field name

# File lib/gorillib/model/field.rb, line 57
def to_s
  name.to_s
end
visibility(meth_type) click to toggle source

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

Protected Instance Methods

inscribe_methods(model) click to toggle source
# 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