diff --git a/Gemfile.lock b/Gemfile.lock index a28490a01..5848aa26b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - kiba-extend (1.4.0) + kiba-extend (1.5.0) activesupport kiba (>= 3.0.0) kiba-common (>= 0.9.0) diff --git a/lib/kiba/extend/transforms/merge.rb b/lib/kiba/extend/transforms/merge.rb index fd6f55dc9..0a8933847 100644 --- a/lib/kiba/extend/transforms/merge.rb +++ b/lib/kiba/extend/transforms/merge.rb @@ -79,7 +79,37 @@ def process(row) end row end - end + end + + class MultivalueConstant + def initialize(on_field:, target:, value:, sep:, placeholder:) + @on_field = on_field + @target = target + @value = value + @sep = sep + @placeholder = placeholder + end + + def process(row) + field_val = row.fetch(@on_field, nil) + if field_val.blank? + row[@target] = @placeholder + return row + end + + merge_vals = [] + field_val.split(@sep, -1).each do |field_val| + if field_val == @placeholder || field_val.blank? + merge_vals << @placeholder + else + merge_vals << @value + end + end + + row[@target] = merge_vals.join(@sep) + row + end + end # used when lookup may return an array of rows from which values should be merged # into the target, AND THE TARGET IS MULTIVALUED diff --git a/lib/kiba/extend/version.rb b/lib/kiba/extend/version.rb index ab3206370..1e3c0509a 100644 --- a/lib/kiba/extend/version.rb +++ b/lib/kiba/extend/version.rb @@ -1,5 +1,5 @@ module Kiba module Extend - VERSION = "1.4.1" + VERSION = "1.5.0" end end diff --git a/spec/kiba/extend/transforms/merge_spec.rb b/spec/kiba/extend/transforms/merge_spec.rb index 6e5166e2b..e2cf39347 100644 --- a/spec/kiba/extend/transforms/merge_spec.rb +++ b/spec/kiba/extend/transforms/merge_spec.rb @@ -157,6 +157,44 @@ end end + describe 'MultivalueConstant' do + test_csv = 'tmp/test.csv' + rows = [ + ['name'], + ['Weddy'], + ['NULL'], + [''], + [nil], + ['Earlybird;Divebomber'], + [';Niblet'], + ['Hunter;'], + ['NULL;Earhart'] + ] + before do + generate_csv(test_csv, rows) + end + it 'adds specified value to new field once per value in specified field' do + expected = [ + { name: 'Weddy', species: 'guinea fowl' }, + { name: 'NULL', species: 'NULL' }, + { name: '', species: 'NULL' }, + { name: nil, species: 'NULL' }, + { name: 'Earlybird;Divebomber', species: 'guinea fowl;guinea fowl' }, + { name: ';Niblet', species: 'NULL;guinea fowl' }, + { name: 'Hunter;', species: 'guinea fowl;NULL' }, + { name: 'NULL;Earhart', species: 'NULL;guinea fowl' } + ] + result = execute_job(filename: test_csv, + xform: Merge::MultivalueConstant, + xformopt: { on_field: :name, + target: :species, + value: 'guinea fowl', + sep: ';', + placeholder: 'NULL'}) + expect(result).to eq(expected) + end + end + describe 'MultiRowLookup' do test_csv = 'tmp/test.csv' rows = [