Class: LittleMapper::Mappers::AR::OneToMany
- Defined in:
- lib/little_mapper/mappers/AR/one_to_many.rb
Instance Attribute Summary collapse
-
#entity_collection_adder ⇒ Object
Returns the value of attribute entity_collection_adder.
-
#entity_field ⇒ Object
Returns the value of attribute entity_field.
-
#mapper ⇒ Object
Returns the value of attribute mapper.
-
#persistent_field ⇒ Object
Returns the value of attribute persistent_field.
-
#persistent_klass ⇒ Object
Returns the value of attribute persistent_klass.
-
#reflexive ⇒ Object
Returns the value of attribute reflexive.
-
#reflexive_setter ⇒ Object
Returns the value of attribute reflexive_setter.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(mapper, entity_field, persistent_klass, opts = {}) ⇒ OneToMany
constructor
A new instance of OneToMany.
- #to_entity(target) ⇒ Object
- #to_persistent(target) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(mapper, entity_field, persistent_klass, opts = {}) ⇒ OneToMany
Returns a new instance of OneToMany.
7 8 9 10 11 12 13 14 15 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 7 def initialize(mapper, entity_field, persistent_klass, opts = {}) @mapper = mapper @entity_field = entity_field @persistent_klass = persistent_klass @persistent_field = opts[:to] || entity_field @entity_collection_adder = opts[:entity_collection_adder] @reflexive = opts.fetch(:reflexive, true) @reflexive_setter = opts[:reflexive_setter] || "#{camel_to_snake(mapper.entity.to_s)}=" end |
Instance Attribute Details
#entity_collection_adder ⇒ Object
Returns the value of attribute entity_collection_adder.
6 7 8 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 6 def entity_collection_adder @entity_collection_adder end |
#entity_field ⇒ Object
Returns the value of attribute entity_field.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 5 def entity_field @entity_field end |
#mapper ⇒ Object
Returns the value of attribute mapper.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 5 def mapper @mapper end |
#persistent_field ⇒ Object
Returns the value of attribute persistent_field.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 5 def persistent_field @persistent_field end |
#persistent_klass ⇒ Object
Returns the value of attribute persistent_klass.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 5 def persistent_klass @persistent_klass end |
#reflexive ⇒ Object
Returns the value of attribute reflexive.
6 7 8 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 6 def reflexive @reflexive end |
#reflexive_setter ⇒ Object
Returns the value of attribute reflexive_setter.
6 7 8 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 6 def reflexive_setter @reflexive_setter end |
Instance Method Details
#to_entity(target) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 22 def to_entity(target) source.__send__(persistent_field).each do |associated| assoc_entity = LittleMapper[persistent_klass].to_entity(associated) if reflexive assoc_entity.__send__(reflexive_setter, target) end target.__send__(entity_field).__send__(:<<, assoc_entity) end end |
#to_persistent(target) ⇒ Object
17 18 19 20 21 |
# File 'lib/little_mapper/mappers/AR/one_to_many.rb', line 17 def to_persistent(target) source.__send__(entity_field).each do |associated| target.__send__(persistent_field).__send__(:<<, LittleMapper[persistent_klass].to_persistent(associated)) end end |