class CDMDEXER::FieldTransformer

Attributes

field_mapping[R]
field_value[R]
formatter_klass[R]

Public Class Methods

new(field_mapping: FieldMapping.new, record: {}, formatter_klass: FieldFormatter) click to toggle source
# File lib/cdmdexer/field_transformer.rb, line 8
def initialize(field_mapping: FieldMapping.new,
               record: {},
               formatter_klass: FieldFormatter)
  @field_mapping   = field_mapping
  @field_value     = compact(record.at_path(origin_path))
  @formatter_klass = formatter_klass
end

Public Instance Methods

reduce() click to toggle source
# File lib/cdmdexer/field_transformer.rb, line 16
def reduce
  (blank?(value)) ? {} : { "#{dest_path}" => value }
end
value() click to toggle source
# File lib/cdmdexer/field_transformer.rb, line 20
def value
  @value ||= (!blank?(field_value)) ? transform_field : nil
end

Private Instance Methods

blank?(val) click to toggle source

File activesupport/lib/active_support/core_ext/object/blank.rb, line 14

# File lib/cdmdexer/field_transformer.rb, line 31
def blank?(val)
  val.respond_to?(:empty?) ? !!val.empty? : !val
end
compact(record) click to toggle source
# File lib/cdmdexer/field_transformer.rb, line 26
def compact(record)
  (record.respond_to?(:compact)) ? record.compact : record
end
transform_field() click to toggle source
# File lib/cdmdexer/field_transformer.rb, line 35
def transform_field
  formatter_klass.new(value: field_value, formatters: formatters).format!
rescue StandardError => e
  raise "Mapping: #{field_mapping.config} Error:#{e.message}"
end