Class: FieldMapper::Custom::Converter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_instance) ⇒ Converter

Returns a new instance of Converter.



11
12
13
14
15
# File 'lib/field_mapper/custom/converter.rb', line 11

def initialize(custom_instance)
  @custom_plat = custom_instance.class
  @custom_instance = custom_instance
  @standard_plat = custom_plat.standard_plat
end

Instance Attribute Details

#custom_instanceObject (readonly)

Returns the value of attribute custom_instance.



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

def custom_instance
  @custom_instance
end

#custom_platObject (readonly)

Returns the value of attribute custom_plat.



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

def custom_plat
  @custom_plat
end

#standard_platObject (readonly)

Returns the value of attribute standard_plat.



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

def standard_plat
  @standard_plat
end

Instance Method Details

#convert_to(custom_plat) ⇒ Object



39
40
41
42
# File 'lib/field_mapper/custom/converter.rb', line 39

def convert_to(custom_plat)
  converter = FieldMapper::Standard::Converter.new(convert_to_standard)
  converter.convert_to(custom_plat)
end

#convert_to_standardObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/field_mapper/custom/converter.rb', line 17

def convert_to_standard
  standard_instance = standard_plat.new

  custom_plat.fields.each do |custom_field_name, custom_field|
    if custom_field.standard_field.present?
      raw_standard_value = get_raw_standard_value(
        custom_field,
        custom_instance[custom_field_name],
        standard_instance
      )
      raw_standard_value = custom_field.standard_field.cast(raw_standard_value)
      standard_instance[custom_field.standard_field.name] = raw_standard_value
    end
  end

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

  standard_instance
end