class CSVConverter::Converters::BaseConverter

Defines the interface that all converters must implement

Attributes

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 contains all the options provided to the converter in the mappings. @return [Hash]

raw_data[R]

@return [String] the raw data of the attribute being processed.

Public Class Methods

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

A new instance of BaseConverter. @param raw_data [String] the raw data of the attribute being processed. @param options [Hash] the options for the converter provided in the mappings.

Additionally, contains the details of the data being processed.
# File lib/csv_converter/converters/base_converter.rb, line 23
def initialize(raw_data, options = {})
  @raw_data   = raw_data.to_s.strip
  @options    = options || {}
end

Public Instance Methods

call() click to toggle source

Converts raw_data into the type specified in the mappings. Must be implemented by children

# File lib/csv_converter/converters/base_converter.rb, line 30
def call
  raise NotImplementedError
end
call!() click to toggle source

Converts raw_data into the type specified in the mappings. Must be implemented by children

# File lib/csv_converter/converters/base_converter.rb, line 36
def call!
  raise NotImplementedError
end
data() click to toggle source

Evaluates raw_data and returns the proper value for it. @return [String]

If raw_data is not empty returns raw_data.
If raw_data is empty and no default value is provided in the mappings returns the nullable object.
If raw_data is empty and a default value is provided in the mappings returns the default value.
# File lib/csv_converter/converters/base_converter.rb, line 45
def data
  @data ||= begin
    return raw_data if raw_data.present? && !empty_value?

    return nullable_object if options.dig(:default).blank?

    options.dig(:default)
  end
end
empty_value?() click to toggle source

Checks if raw_data is contained in the list of empty_values provided in the mapping. @return [Boolean]

# File lib/csv_converter/converters/base_converter.rb, line 57
def empty_value?
  return false unless options.dig(:empty_values)

  options.dig(:empty_values).include?(raw_data)
end

Private Instance Methods

nullable_object() click to toggle source
# File lib/csv_converter/converters/base_converter.rb, line 65
def nullable_object
  raise NotImplementedError
end