Class: CSVConverter::EntityProcessor

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

Overview

Iterates over the columns of a row and processes the data accordingly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, entity_mappings, options = {}) ⇒ EntityProcessor

A new instance of EntityProcessor.

Parameters:



28
29
30
31
32
# File 'lib/csv_converter/entity_processor.rb', line 28

def initialize(row, entity_mappings, options = {})
  @row              = row
  @entity_mappings  = entity_mappings
  @options          = options
end

Instance Attribute Details

#entity_mappingsHash (readonly)

Entity mappings.

Returns:

  • (Hash)

    configuration used to process the data.



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

def entity_mappings
  @entity_mappings
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)


22
23
24
# File 'lib/csv_converter/entity_processor.rb', line 22

def options
  @options
end

#rowArray, Hash (readonly)

The row being processed.

Returns:

  • (Array, Hash)


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

def row
  @row
end

Instance Method Details

#callHash

Iterates over the attributes of each entity converting the data into the format expected by the mappings.

Returns:

  • (Hash)

    the attributes for each entity. If an error occurs while processing the error is rescued and nullable values returned for the attributes that caused the error.



38
39
40
# File 'lib/csv_converter/entity_processor.rb', line 38

def call
  entity_attrs(&:call)
end

#call!Hash

Iterates over the attributes of each entity converting the data into the format expected by the mappings.

Returns:

  • (Hash)

    the attributes for each entity. If an error occurs while processing the an error is raised.



45
46
47
# File 'lib/csv_converter/entity_processor.rb', line 45

def call!
  entity_attrs(&:call!)
end