Class: Gorillib::Model::Field
- Includes:
- Gorillib::Model
- Defined in:
- lib/gorillib/model/field.rb
Overview
Represents a field for reflection
Direct Known Subclasses
Builder::GetsetField, Builder::MemberField, SimpleCollectionField
Instance Attribute Summary collapse
-
#_extra_attributes ⇒ Object
readonly
[Hash] all options passed to the field not recognized by one of its own current fields.
-
#doc ⇒ String
The doc field field :doc, String, doc: "Field's description".
-
#model ⇒ Object
readonly
[Gorillib::Model] Model owning this field.
-
#name ⇒ String
The name field field :name, String, position: 0, writer: false, doc: "The field name.
-
#type ⇒ Class
The type field field :type, Class, position: 1, doc: "Factory to generate field's values".
Class Method Summary collapse
Instance Method Summary collapse
- #==(val) ⇒ Object
-
#initialize(model, name, type, options = {}) ⇒ Field
constructor
A new instance of Field.
-
#inspect ⇒ String
Human-readable presentation of the field definition.
- #inspect_compact ⇒ Object
- #to_hash ⇒ Object
-
#to_s ⇒ String
The field name.
-
#visibility(meth_type) ⇒ Object
returns the visibility.
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.
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, ={}) Validate.identifier!(name) type_opts = .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( .extract!(*default_visibilities.keys).compact ) @doc = .delete(:name){ "#{name} field" } receive!() end |
Instance Attribute Details
#_extra_attributes ⇒ Object (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 |
#doc ⇒ String
Returns 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" |
#model ⇒ Object (readonly)
[Gorillib::Model] Model owning this field
15 16 17 |
# File 'lib/gorillib/model/field.rb', line 15 def model @model end |
#name ⇒ String
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)".
25 26 27 |
# File 'lib/gorillib/model/field.rb', line 25 def name @name end |
#type ⇒ Class
Returns 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 |
#inspect ⇒ String
Returns 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_compact ⇒ Object
66 67 68 |
# File 'lib/gorillib/model/field.rb', line 66 def inspect_compact "field(#{name})" end |
#to_hash ⇒ Object
70 71 72 |
# File 'lib/gorillib/model/field.rb', line 70 def to_hash attributes.merge!(@visibility).merge!(@_extra_attributes) end |
#to_s ⇒ String
Returns the field name.
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
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 |