Class: LittleMapper::Mappers::AR::OneToMany

Inherits:
Base
  • Object
show all
Defined in:
lib/little_mapper/mappers/AR/one_to_many.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#source

Instance Method Summary collapse

Methods inherited from Base

#camel_to_snake, #from

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_adderObject

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_fieldObject

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

#mapperObject

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_fieldObject

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_klassObject

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

#reflexiveObject

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_setterObject

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