Class: ArcFurnace::BinaryKeyMergingHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/arc-furnace/binary_key_merging_hash.rb

Overview

This allows one to merge multiple rows into one such as:

key, attribute, value
  1, value1, foo
  1, value1, bar
  1, value2, baz

Results in:

1 => {  value1 => [foo, bar], value2 => baz }

Instance Attribute Summary

Attributes inherited from Hash

#key_column

Attributes inherited from Node

#error_handler, #node_id, #params

Instance Method Summary collapse

Methods inherited from Hash

#each, #finalize, #get

Constructor Details

#initialize(source:, primary_key:, secondary_key:, value_key:) ⇒ BinaryKeyMergingHash

Returns a new instance of BinaryKeyMergingHash.



14
15
16
17
18
# File 'lib/arc-furnace/binary_key_merging_hash.rb', line 14

def initialize(source: , primary_key:, secondary_key:, value_key:)
  super(source: source, key_column: primary_key)
  @secondary_key = secondary_key
  @value_key = value_key
end

Instance Method Details

#prepareObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/arc-furnace/binary_key_merging_hash.rb', line 20

def prepare
  loop do
    break if source.empty?
    row = source.row
    row_key = row[key_column]
    second_key = row[secondary_key]
    value = row[value_key]
    if row_key && second_key && value
      row_entry = hash[row_key] ||= {}
      value_arr = row_entry[second_key] ||= []
      value_arr.concat(Array.wrap(value))
    else
      error_handler.missing_primary_key(source_row: row, node_id: node_id)
    end
  end
end