Class: Kiba::Extend::Transforms::Merge::MultiRowLookup
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Merge::MultiRowLookup
- Defined in:
- lib/kiba/extend/transforms/merge/multi_row_lookup.rb
Overview
Merge one or more rows from a Utils::LookupHash into source data, matching on
keycolumn
values
Defined Under Namespace
Classes: EmptyFieldmap, LookupTypeError
Instance Method Summary collapse
-
#initialize(fieldmap:, lookup:, keycolumn:, constantmap: {}, conditions: {}, multikey: false, delim: Kiba::Extend.delim, null_placeholder: nil, sorter: nil) ⇒ MultiRowLookup
constructor
A new instance of MultiRowLookup.
-
#process(row) ⇒ Object
Constructor Details
#initialize(fieldmap:, lookup:, keycolumn:, constantmap: {}, conditions: {}, multikey: false, delim: Kiba::Extend.delim, null_placeholder: nil, sorter: nil) ⇒ MultiRowLookup
Note:
Interaction of specifying a sorter
and multikey: true
may be unexpected.
Returns a new instance of MultiRowLookup.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/kiba/extend/transforms/merge/multi_row_lookup.rb', line 56 def initialize(fieldmap:, lookup:, keycolumn:, constantmap: {}, conditions: {}, multikey: false, delim: Kiba::Extend.delim, null_placeholder: nil, sorter: nil) @fieldmap = fieldmap # hash of looked-up values to merge in for each merged-in row fail EmptyFieldmap if fieldmap.empty? @constantmap = constantmap # hash of constants to add for each merged-in row @lookup = lookup # lookuphash; should be created with csv_to_multi_hash fail LookupTypeError.new(lookup) unless lookup.is_a?(Hash) @keycolumn = keycolumn # column in main table containing value expected to be lookup key @multikey = multikey # should the key be treated as multivalued @conditions = conditions @delim = delim @null_placeholder = null_placeholder @sort_on = sort_on @sort_dir = sort_dir @selector = Lookup::RowSelector.call( conditions: conditions, sep: delim ) @sorter = sorter end |
Instance Method Details
#process(row) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/kiba/extend/transforms/merge/multi_row_lookup.rb', line 81 def process(row) field_data = Kiba::Extend::Utils::Fieldset.new( fields: fieldmap.values, null_placeholder: null_placeholder ) id_data = row.fetch(keycolumn, "") id_data = id_data.nil? ? "" : id_data ids = multikey ? id_data.split(delim) : [id_data] ids.each do |id| rtm = rows_to_merge(id, row) field_data.populate(rtm) end constantmap.each do |field, value| field_data.add_constant_values(field, value) end field_data.join_values(delim) field_data.hash.each do |field, value| row[target_field(field)] = value.blank? ? nil : value end row end |