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