class FieldMapper::Standard::Converter

Attributes

standard_instance[R]
standard_plat[R]

Public Class Methods

new(standard_instance) click to toggle source
# File lib/field_mapper/standard/converter.rb, line 7
def initialize(standard_instance)
  @standard_plat = standard_instance.class
  @standard_instance = standard_instance
end

Public Instance Methods

convert_to(custom_plat, memoize: true) click to toggle source
# File lib/field_mapper/standard/converter.rb, line 12
def convert_to(custom_plat, memoize: true)
  @converted_to_custom ||= {}
  @converted_to_custom[custom_plat] = nil unless memoize
  @converted_to_custom[custom_plat] ||= begin
    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
end

Protected Instance Methods

compute_raw_custom_value(custom_field, raw_standard_value, custom_instance) click to toggle source
# File lib/field_mapper/standard/converter.rb, line 89
def compute_raw_custom_value(custom_field, raw_standard_value, custom_instance)
  raw_custom_value = custom_instance.instance_exec(
    raw_standard_value,
    standard_instance: standard_instance,
    &custom_field.standard_to_custom
  )

  if !raw_custom_value.nil?
    raw_custom_value = custom_field.cast(raw_custom_value)
  end
  raw_custom_value
end
compute_raw_custom_value_for_plat(custom_field, raw_standard_value) click to toggle source
# File lib/field_mapper/standard/converter.rb, line 108
def compute_raw_custom_value_for_plat(custom_field, raw_standard_value)
  converter = FieldMapper::Standard::Converter.new(raw_standard_value)
  converter.convert_to(custom_field.type.type)
end
compute_raw_custom_value_for_plat_list(custom_field, raw_standard_values) click to toggle source
# File lib/field_mapper/standard/converter.rb, line 102
def compute_raw_custom_value_for_plat_list(custom_field, raw_standard_values)
  raw_standard_values.map do |raw_standard_value|
    compute_raw_custom_value_for_plat(custom_field, raw_standard_value)
  end
end
find_raw_custom_value(custom_field, raw_standard_value) click to toggle source
# File lib/field_mapper/standard/converter.rb, line 75
def find_raw_custom_value(custom_field, raw_standard_value)
  return raw_standard_value unless custom_field.has_values?

  if !custom_field.standard_field.has_values?
    custom_value = custom_field.find_value(raw_standard_value)
    return nil if custom_value.nil?
    return custom_value.value
  end

  custom_value = custom_field.find_priority_value_mapped_to_standard(raw_standard_value)
  return nil if custom_value.nil?
  custom_value.value
end
get_raw_custom_value(custom_field, raw_standard_value, custom_instance) click to toggle source
# File lib/field_mapper/standard/converter.rb, line 43
def get_raw_custom_value(custom_field, raw_standard_value, custom_instance)
  strategy = custom_field.flip_strategy(:standard_to_custom)
  custom_flipper = custom_field.custom_flipper?(:standard_to_custom)

  if !custom_flipper
    if raw_standard_value.nil?
      return [] if custom_field.list_with_emtpy_default?
      return nil
    end

    if custom_field.plat?
      return compute_raw_custom_value_for_plat(custom_field, raw_standard_value)
    end

    if custom_field.plat_list?
      return compute_raw_custom_value_for_plat_list(custom_field, raw_standard_value)
    end
  end

  if strategy == :find
    if custom_field.list?
      return raw_standard_value.map do |single_raw_standard_value|
        find_raw_custom_value(custom_field, single_raw_standard_value)
      end
    else
      return find_raw_custom_value(custom_field, raw_standard_value)
    end
  end

  compute_raw_custom_value(custom_field, raw_standard_value, custom_instance)
end