Module: Minimapper::Entity::HasMany

Included in:
Attributes::ClassMethods
Defined in:
lib/minimapper/entity/has_many.rb

Instance Method Summary collapse

Instance Method Details

#has_many(name, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/minimapper/entity/has_many.rb', line 6

def has_many(name, opts = {})
  serialize = opts.fetch(:serialize, false)

  attr_writer name

  if serialize
    attribute_name = "#{name}_attributes"
    attribute attribute_name

    setup_has_many(name) do
      attributes = send(attribute_name)
      entity_class = name.to_s.singularize.camelcase.constantize
      (attributes || []).map { |attr| entity_class.new(attr) }
    end
  else
    setup_has_many(name) { [] }
  end
end