Class: LittleMapper::Mappers::AR::OneToOne
- Defined in:
- lib/little_mapper/mappers/AR/one_to_one.rb
Instance Attribute Summary collapse
-
#entity_setter ⇒ Object
Returns the value of attribute entity_setter.
-
#field ⇒ Object
Returns the value of attribute field.
-
#mapper ⇒ Object
Returns the value of attribute mapper.
-
#persistent_entity_setter ⇒ Object
Returns the value of attribute persistent_entity_setter.
-
#persistent_field ⇒ Object
Returns the value of attribute persistent_field.
-
#persistent_klass ⇒ Object
Returns the value of attribute persistent_klass.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(mapper, field, opts = {}) ⇒ OneToOne
constructor
A new instance of OneToOne.
- #to_entity(target) ⇒ Object
- #to_persistent(target) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(mapper, field, opts = {}) ⇒ OneToOne
Returns a new instance of OneToOne.
6 7 8 9 10 11 12 13 |
# File 'lib/little_mapper/mappers/AR/one_to_one.rb', line 6 def initialize(mapper, field, opts = {}) @mapper = mapper @field = field @persistent_klass = opts[:as] @persistent_field = opts[:to] || field @entity_setter = opts[:entity_setter] || "#{field}=".to_sym @persistent_entity_setter = opts[:persistent_entity_setter] || "#{persistent_field}=".to_sym end |
Instance Attribute Details
#entity_setter ⇒ Object
Returns the value of attribute entity_setter.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_one.rb', line 5 def entity_setter @entity_setter end |
#field ⇒ Object
Returns the value of attribute field.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_one.rb', line 5 def field @field end |
#mapper ⇒ Object
Returns the value of attribute mapper.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_one.rb', line 5 def mapper @mapper end |
#persistent_entity_setter ⇒ Object
Returns the value of attribute persistent_entity_setter.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_one.rb', line 5 def persistent_entity_setter @persistent_entity_setter end |
#persistent_field ⇒ Object
Returns the value of attribute persistent_field.
5 6 7 |
# File 'lib/little_mapper/mappers/AR/one_to_one.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_one.rb', line 5 def persistent_klass @persistent_klass end |
Instance Method Details
#to_entity(target) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/little_mapper/mappers/AR/one_to_one.rb', line 26 def to_entity(target) if persistent_klass val = source.__send__(persistent_field) if val target.__send__(entity_setter, LittleMapper[persistent_klass].to_entity(val)) end else target.__send__(entity_setter, source.__send__(persistent_field)) end end |
#to_persistent(target) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/little_mapper/mappers/AR/one_to_one.rb', line 15 def to_persistent(target) if persistent_klass val = source.__send__(field) if val target.__send__(persistent_entity_setter, LittleMapper[persistent_klass].to_persistent(val)) end else target.__send__(persistent_entity_setter, source.__send__(field)) end end |