Class: Cascade::RowProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/cascade/row_processor.rb

Constant Summary collapse

DEFAULT_PROCESSOR =
->(value) { value&.to_s&.strip }

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RowProcessor

Returns a new instance of RowProcessor.



13
14
15
16
17
18
19
# File 'lib/cascade/row_processor.rb', line 13

def initialize(options = {})
  @options = options
  @ext_presenters = options[:ext_presenters].to_h
  @columns_matching = options.fetch(:columns_matching)
  @use_default_presenter = options.fetch(:use_default_presenter, false)
  @deafult_presenter = options.fetch(:deafult_presenter, DEFAULT_PROCESSOR)
end

Instance Method Details

#call(row) ⇒ Hash

Iterates through object using columns values supported keys as keys for iterating, then parse it by the corresponding parser.

Parameters:

  • row (Hash)

    the object retrieved from data parser

Returns:

  • (Hash)

    the object with parsed columns



26
27
28
29
30
31
32
# File 'lib/cascade/row_processor.rb', line 26

def call(row)
  @columns_matching.supported_keys.inject({}) do |result, key|
    raw_value = row.fetch(@columns_matching.index(key))
    value     = receive_presenter(key).call(raw_value)
    result.merge(key => value)
  end
end