class CSVConverter::AttributeProcessor

Extracts the value for a column from the csv row and applies conversions to it.

Attributes

attr_mappings[R]

Attribute mappings. @return [Hash] configuration used to process the data.

data[R]

The column being processed. @return [String]

options[R]

Details of the data being processed. By default this includes:

filename: the name of the file being processed.
row_num: number of the row being processed.
entity: the name of the entity being processed as provided in the mappings.
row: the raw data of the row being processed.
attr: the name of the attribute being processed as provided in the mappings.

Additionally it will contain all the options provided to the converter in the mappings. @return [Hash]

row[R]

The row being processed. @return [Array, Hash]

Public Class Methods

new(row, attr_mappings, options = {}) click to toggle source

A new instance of AttributeProcessor. @param row (@see row) @param attr_mappings (@see attr_mappings) @param options (@see options)

# File lib/csv_converter/attribute_processor.rb, line 32
def initialize(row, attr_mappings, options = {})
  @row            = row
  @attr_mappings  = attr_mappings
  @options        = options
  @data           = row[attr_mappings[:header]]
end

Public Instance Methods

call() click to toggle source

Converts the data of the attribute into the type provided in the mappings by invoking the converters. @return [Object] an object of the expected type.

If an error occurs during conversion the nullable value for the attributes is returned.
# File lib/csv_converter/attribute_processor.rb, line 42
def call
  convert_attr(&:call)
end
call!() click to toggle source

Converts the data of the attribute into the type provided in the mappings by invoking the converters. @return [Object] an object of the expected type.

If an error occurs during conversion an error is raised.
# File lib/csv_converter/attribute_processor.rb, line 49
def call!
  convert_attr(&:call!)
end

Private Instance Methods

convert_attr(&block) click to toggle source
# File lib/csv_converter/attribute_processor.rb, line 55
def convert_attr(&block)
  return data if attr_mappings.dig(:converters).blank?

  attr_mappings.dig(:converters).inject(data) do |d, (converter, opts)|
    opts = (opts || {}).merge(options)
    invoke_converter(converter, d, opts, &block)
  end
end
invoke_converter(converter, data, opts) { |converter| ... } click to toggle source
# File lib/csv_converter/attribute_processor.rb, line 64
def invoke_converter(converter, data, opts)
  return converter.call(data, opts) if converter.is_a?(Proc)

  converter = converter.to_s
  converter = CSVConverter::ALIASES[converter] if CSVConverter::ALIASES.keys.include?(converter)
  converter = converter.constantize.new(data, opts)

  yield(converter)
end