Class: CsvPiper::Processors::Copy

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_piper/processors/copy.rb

Overview

Use to copy data from source row to transformed hash. Does not add any errors.

Instance Method Summary collapse

Constructor Details

#initialize(mapping = nil) ⇒ Copy

Returns a new instance of Copy.

Parameters:

  • mapping: (nil, Array, Hash{source_key => new_key})

    (Defaults to nil)

    • When nil: All contents of the source hash will be copied across to the transformed hash

    • When an Array: Only the matching keys will be copied to the transformed hash

    • When a Hash: Only the matching keys will be copied to the transformed hash but they will be copied onto the transformed hash with a new key value (mapping = { source_key => new_key } )



10
11
12
13
# File 'lib/csv_piper/processors/copy.rb', line 10

def initialize(mapping = nil)
  mapping = Hash[ mapping.map { |val| [val, val] } ] if mapping.is_a?(Array)
  @mapping = mapping
end

Instance Method Details

#process(source, transformed, errors) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/csv_piper/processors/copy.rb', line 15

def process(source, transformed, errors)
  if @mapping.is_a?(Hash)
    transformed = @mapping.each_with_object(transformed) do |(key, new_key), memo|
      memo[new_key] = source[key]
    end
  else
    transformed = transformed.merge(source)
  end
  [transformed, errors]
end