Sha256: 4180fe1602c3ec431160795dc5bba3396fd9e6090c906918b135c5cc8cdb1f03

Contents?: true

Size: 1.07 KB

Versions: 40

Compression:

Stored size: 1.07 KB

Contents

require 'arc-furnace/hash'

module ArcFurnace
  # 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 }
  class BinaryKeyMergingHash < ::ArcFurnace::Hash
    private_attr_reader :source, :hash, :secondary_key, :value_key

    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

    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

  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
arc-furnace-0.1.42 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.41 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.40 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.39 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.38 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.37 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.36 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.35 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.34 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.33 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.32 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.31 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.30 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.29 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.28 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.27 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.26 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.25 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.24 lib/arc-furnace/binary_key_merging_hash.rb
arc-furnace-0.1.23 lib/arc-furnace/binary_key_merging_hash.rb