Class: FieldMapper::Standard::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/field_mapper/standard/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(standard_instance) ⇒ Converter

Returns a new instance of Converter.



7
8
9
10
# File 'lib/field_mapper/standard/converter.rb', line 7

def initialize(standard_instance)
  @standard_plat = standard_instance.class
  @standard_instance = standard_instance
end

Instance Attribute Details

#standard_instanceObject (readonly)

Returns the value of attribute standard_instance.



5
6
7
# File 'lib/field_mapper/standard/converter.rb', line 5

def standard_instance
  @standard_instance
end

#standard_platObject (readonly)

Returns the value of attribute standard_plat.



5
6
7
# File 'lib/field_mapper/standard/converter.rb', line 5

def standard_plat
  @standard_plat
end

Instance Method Details

#convert_to(custom_plat) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/field_mapper/standard/converter.rb', line 12

def convert_to(custom_plat)
  raise StandardMismatch if custom_plat.standard_plat != standard_plat

  custom_instance = custom_plat.new

  standard_plat.fields.each do |standard_field_name, standard_field|
    custom_fields = custom_plat.find_mapped_fields(standard_field)
    custom_fields.each do |custom_field|
      raw_custom_value = get_raw_custom_value(
        custom_field,
        standard_instance[standard_field_name],
        custom_instance
      )
      raw_custom_value = custom_field.cast(raw_custom_value)
      custom_instance[custom_field.name] = raw_custom_value
    end
  end

  [standard_instance, custom_instance].each do |instance|
    instance.send(:after_convert, from: standard_instance, to: custom_instance)
  end

  custom_instance
end