Sha256: 0f3a1a3a2f987f2d45456db6333df259859f3e2fbc0fab3087ec7db5ec180163

Contents?: true

Size: 1.14 KB

Versions: 40

Compression:

Stored size: 1.14 KB

Contents

module ArcFurnace
  # This allows one to merge multiple rows into one such as:
  #   key, value
  #     1, foo
  #     1, bar
  #     2, baz
  # Results in:
  #   1 => { key => 1, value: [foo, bar] }
  #   2 => { key => 2, value: baz }
  class MergingHash < ::ArcFurnace::Hash
    private_attr_reader :source, :hash

    def prepare
      loop do
        break if source.empty?
        row = source.row
        row_key = row[key_column]
        if row_key
          row_entry = hash[row_key] ||= {}
          row.each do |column, values|
            existing_column_values = row_entry[column]
            if existing_column_values && column != key_column
              if existing_column_values.is_a?(Array)
                existing_column_values.concat(Array.wrap(values))
              else
                new_row_entry = Array.wrap(existing_column_values)
                new_row_entry.concat(Array.wrap(values))
                row_entry[column] = new_row_entry
              end
            else
              unless values.nil?
                row_entry[column] = values.dup
              end
            end
          end
        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/merging_hash.rb
arc-furnace-0.1.41 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.40 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.39 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.38 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.37 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.36 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.35 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.34 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.33 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.32 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.31 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.30 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.29 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.28 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.27 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.26 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.25 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.24 lib/arc-furnace/merging_hash.rb
arc-furnace-0.1.23 lib/arc-furnace/merging_hash.rb