Class: CSVConverter::AttributeProcessor

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, attr_mappings, options = {}) ⇒ AttributeProcessor

A new instance of AttributeProcessor.

Parameters:



32
33
34
35
36
37
# 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

Instance Attribute Details

#attr_mappingsHash (readonly)

Attribute mappings.

Returns:

  • (Hash)

    configuration used to process the data.



16
17
18
# File 'lib/csv_converter/attribute_processor.rb', line 16

def attr_mappings
  @attr_mappings
end

#dataString (readonly)

The column being processed.

Returns:

  • (String)


12
13
14
# File 'lib/csv_converter/attribute_processor.rb', line 12

def data
  @data
end

#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 will contain all the options provided to the converter in the mappings.

Returns:

  • (Hash)


26
27
28
# File 'lib/csv_converter/attribute_processor.rb', line 26

def options
  @options
end

#rowArray, Hash (readonly)

The row being processed.

Returns:

  • (Array, Hash)


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

def row
  @row
end

Instance Method Details

#callObject

Converts the data of the attribute into the type provided in the mappings by invoking the converters.

Returns:

  • (Object)

    an object of the expected type. If an error occurs during conversion the nullable value for the attributes is returned.



42
43
44
# File 'lib/csv_converter/attribute_processor.rb', line 42

def call
  convert_attr(&:call)
end

#call!Object

Converts the data of the attribute into the type provided in the mappings by invoking the converters.

Returns:

  • (Object)

    an object of the expected type. If an error occurs during conversion an error is raised.



49
50
51
# File 'lib/csv_converter/attribute_processor.rb', line 49

def call!
  convert_attr(&:call!)
end