Class: CSVConverter::Converters::BaseConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_converter/converters/base_converter.rb

Overview

Defines the interface that all converters must implement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data, options = {}) ⇒ BaseConverter

A new instance of BaseConverter.

Parameters:

  • raw_data (String)

    the raw data of the attribute being processed.

  • options (Hash) (defaults to: {})

    the options for the converter provided in the mappings. Additionally, contains the details of the data being processed.



23
24
25
26
# 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

Instance Attribute Details

#optionsHash (readonly)

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.

Returns:

  • (Hash)


17
18
19
# File 'lib/csv_converter/converters/base_converter.rb', line 17

def options
  @options
end

#raw_dataString (readonly)

Returns the raw data of the attribute being processed.

Returns:

  • (String)

    the raw data of the attribute being processed.



8
9
10
# File 'lib/csv_converter/converters/base_converter.rb', line 8

def raw_data
  @raw_data
end

Instance Method Details

#callObject

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

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/csv_converter/converters/base_converter.rb', line 30

def call
  raise NotImplementedError
end

#call!Object

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

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/csv_converter/converters/base_converter.rb', line 36

def call!
  raise NotImplementedError
end

#dataString

Evaluates raw_data and returns the proper value for it.

Returns:

  • (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.



45
46
47
48
49
50
51
52
53
# 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?Boolean

Checks if raw_data is contained in the list of empty_values provided in the mapping.

Returns:

  • (Boolean)


57
58
59
60
61
# 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