Class: LittleMapper::Mappers::AR::OneToOne

Inherits:
Base
  • Object
show all
Defined in:
lib/little_mapper/mappers/AR/one_to_one.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, 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_setterObject

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

#fieldObject

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

#mapperObject

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_setterObject

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_fieldObject

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_klassObject

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