Class: CSVConverter::FileProcessor

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

Overview

Iterates over a collection and processes the data accordingly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, rows, file_mappings) ⇒ FileProcessor

A new instance of FileProcessor.

Parameters:



22
23
24
25
26
# File 'lib/csv_converter/file_processor.rb', line 22

def initialize(filename, rows, file_mappings)
  @filename       = filename
  @rows           = rows
  @file_mappings  = file_mappings.with_indifferent_access
end

Instance Attribute Details

#file_mappingsHash (readonly)

File mappings.

Returns:

  • (Hash)

    configuration used to process the data.



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

def file_mappings
  @file_mappings
end

#filenameString (readonly)

Name of the file being processed.

Returns:

  • (String)

    the name of the file.



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

def filename
  @filename
end

#rowsArray (readonly)

Collection being processed.

Returns:

  • (Array)

    collection of rows being processed.



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

def rows
  @rows
end

Instance Method Details

#callArray

Iterates over the rows grouping and converting the data as expected based on the mappings.

Returns:

  • (Array)

    Collection of hashes containing the entities obtained after processing the rows. If an error occurs while processing the error is rescued and nullable values returned for the attributes that caused the error.



32
33
34
35
36
# File 'lib/csv_converter/file_processor.rb', line 32

def call
  rows.map.with_index do |row, row_num|
    process_entities(row, row_num, &:call)
  end
end

#call!Array

Iterates over the rows grouping and converting the data as expected based on the mappings.

Returns:

  • (Array)

    Collection of hashes containing the entities obtained after processing the rows. If an error occurs while processing the rows an error is raised.



41
42
43
44
45
# File 'lib/csv_converter/file_processor.rb', line 41

def call!
  rows.map.with_index do |row, row_num|
    process_entities(row, row_num, &:call!)
  end
end