Class: Gorillib::Model::Field

Inherits:
Object
  • Object
show all
Includes:
Gorillib::Model
Defined in:
lib/gorillib/model/field.rb

Overview

Represents a field for reflection

Examples:

Usage

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gorillib::Model

#as_json, #attribute_set?, #attribute_values, #attributes, #compact_attributes, #handle_extra_attributes, #read_attribute, #read_unset_attribute, #receive!, #to_inspectable, #to_json, #to_tsv, #to_wire, #unset_attribute, #update_attributes, #write_attribute

Methods included from Concern

#append_features, extended, #included

Constructor Details

#initialize(model, name, type, options = {}) ⇒ Field

Returns a new instance of Field.

Parameters:

  • name (#to_sym)

    Field name

  • type (#receive)

    Factory for field values. To accept any object as-is, specify Object as the type.

  • model (Gorillib::Model)

    Field's owner

  • options (Hash{Symbol => Object}) (defaults to: {})

    Extended attributes

Options Hash (options):

  • doc (String)

    Description of the field's purpose

  • :reader (true, false, :public, :protected, :private)

    Visibility for the reader (#foo) method; false means don't create one.

  • :writer (true, false, :public, :protected, :private)

    Visibility for the writer (#foo=) method; false means don't create one.

  • :receiver (true, false, :public, :protected, :private)

    Visibility for the receiver (#receive_foo) method; false means don't create one.



40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#_extra_attributesObject (readonly)

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



18
19
20
# File 'lib/gorillib/model/field.rb', line 18

def _extra_attributes
  @_extra_attributes
end

#docString

Returns the doc field field :doc, String, doc: "Field's description".

Returns:

  • (String)

    the doc field field :doc, String, doc: "Field's description"



124
# File 'lib/gorillib/model/field.rb', line 124

field :doc,  String,                             doc: "Field's description"

#modelObject (readonly)

[Gorillib::Model] Model owning this field



15
16
17
# File 'lib/gorillib/model/field.rb', line 15

def model
  @model
end

#nameString

Returns the name field field :name, String, position: 0, writer: false, doc: "The field name. Must start with [A-Za-z_] and subsequently contain only [A-Za-z0-9_] (required)".

Returns:

  • (String)

    the name field field :name, String, position: 0, writer: false, doc: "The field name. Must start with [A-Za-z_] and subsequently contain only [A-Za-z0-9_] (required)"



25
26
27
# File 'lib/gorillib/model/field.rb', line 25

def name
  @name
end

#typeClass

Returns the type field field :type, Class, position: 1, doc: "Factory to generate field's values".

Returns:

  • (Class)

    the type field field :type, Class, position: 1, doc: "Factory to generate field's values"



26
27
28
# File 'lib/gorillib/model/field.rb', line 26

def type
  @type
end

Class Method Details

.receive(hsh) ⇒ Object



78
79
80
81
82
83
# 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

Instance Method Details

#==(val) ⇒ Object



74
75
76
# File 'lib/gorillib/model/field.rb', line 74

def ==(val)
  super && (val._extra_attributes == self._extra_attributes) && (val.model == self.model)
end

#inspectString

Returns Human-readable presentation of the field definition.

Returns:

  • (String)

    Human-readable presentation of the field definition



62
63
64
65
# 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_compactObject



66
67
68
# File 'lib/gorillib/model/field.rb', line 66

def inspect_compact
  "field(#{name})"
end

#to_hashObject



70
71
72
# File 'lib/gorillib/model/field.rb', line 70

def to_hash
  attributes.merge!(@visibility).merge!(@_extra_attributes)
end

#to_sString

Returns the field name.

Returns:



57
58
59
# File 'lib/gorillib/model/field.rb', line 57

def to_s
  name.to_s
end

#visibility(meth_type) ⇒ Object

returns the visibility

Examples:

reader is protected, no writer:

Foo.field :granuloxity, :reader => :protected, :writer => false


91
92
93
94
# 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