class ArrayTransform::Operations::JoinWithLoookup
Attributes
column_header[R]
data[R]
lookup_by_key[R]
lookup_column_header[R]
lookup_data[R]
Public Class Methods
new( column_header: nil, column_index: nil, data:, lookup_data:, lookup_column_header: nil, lookup_column_index: nil, lookup_default: nil )
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 6 def initialize( column_header: nil, column_index: nil, data:, lookup_data:, lookup_column_header: nil, lookup_column_index: nil, lookup_default: nil ) @column_header = column_header @column_index = column_index @data = data @lookup_column_header = lookup_column_header @lookup_column_index = lookup_column_index @lookup_data = lookup_data @lookup_default = lookup_default end
Public Instance Methods
call()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 26 def call validate! data.size > 0 && index_lookup_data join_lookup_data end
Private Instance Methods
column_header_index()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 40 def column_header_index data[0] && data[0].index(column_header) end
column_index()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 36 def column_index @column_index ||= column_header_index end
index_lookup_data()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 44 def index_lookup_data @lookup_by_key ||= begin lookup_data.inject({}) { |map, row| map.tap { map[row[lookup_column_index]] = row.dup.tap { |row_copy| row_copy.delete_at(lookup_column_index) } } } end end
join_lookup_data()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 54 def join_lookup_data data.each_with_index do |row, row_index| if row_index == 0 data[row_index] = data[row_index] + lookup_data[0].dup.tap { |row_copy| row_copy.delete_at(lookup_column_index) } else row_key = data[row_index][column_index] data[row_index] = data[row_index] + (lookup_by_key[row_key] || lookup_default) end end end
lookup_column_header_index()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 69 def lookup_column_header_index lookup_data[0] && lookup_data[0].index(lookup_column_header) end
lookup_column_index()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 65 def lookup_column_index @lookup_column_index ||= lookup_column_header_index end
lookup_default()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 73 def lookup_default @lookup_default ||= [nil] * (lookup_data[0].count - 1) end
validate!()
click to toggle source
# File lib/array_transform/operations/join_with_lookup.rb, line 77 def validate! if @column_index && @column_header raise(ArgumentError, "can only specify one of :column_index or :column_header") end if @lookup_column_index && @lookup_column_header raise(ArgumentError, "can only specify one of :lookup_column_index or :lookup_column_header") end if @lookup_default && lookup_data[0] && @lookup_default.count != lookup_data[0].count - 1 raise(ArgumentError, "lookup_default must have exactly #{lookup_data[0].count - 1} columns") end end