Class: Gorillib::Model::SimpleCollectionField

Inherits:
Field show all
Defined in:
lib/gorillib/model/field.rb

Direct Known Subclasses

Builder::GetsetCollectionField

Instance Attribute Summary collapse

Attributes inherited from Field

#_extra_attributes, #doc, #model, #name, #type

Instance Method Summary collapse

Methods inherited from Field

#==, #inspect, #inspect_compact, receive, #to_hash, #to_s, #visibility

Methods included from Gorillib::Model

#==, #as_json, #attribute_set?, #attribute_values, #attributes, #compact_attributes, #handle_extra_attributes, #inspect, #inspect_compact, #read_attribute, #read_unset_attribute, #receive!, #to_inspectable, #to_json, #to_s, #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 = {}) ⇒ SimpleCollectionField

field :collection_attrs, Hash, default: Hash.new, doc: "Extra attributes to pass to the collection on creation -- eg. key_method"



136
137
138
139
140
141
142
143
# File 'lib/gorillib/model/field.rb', line 136

def initialize(model, name, type, options={})
  super
  collection_type = self.type
  item_type       = self.item_type
  key_method      = options[:key_method] if options[:key_method]
  raise "Please supply an item type for #{self.inspect} -- eg 'collection #{name.inspect}, of: FooClass'" unless item_type
  self.default ||= ->{ collection_type.new(item_type: item_type, belongs_to: self, key_method: key_method) }
end

Instance Attribute Details

#item_typeClass

Returns the item_type field field :item_type, Class, default: Whatever, doc: "Factory for collection items".

Returns:

  • (Class)

    the item_type field field :item_type, Class, default: Whatever, doc: "Factory for collection items"



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

field :item_type,        Class, default: Whatever, doc: "Factory for collection items"

Instance Method Details

#inscribe_methods(model) ⇒ Object



145
146
147
148
# File 'lib/gorillib/model/field.rb', line 145

def inscribe_methods(model)
  super
  model.__send__(:define_collection_receiver, self)
end