From 17c896915365055d145cc8c454d4c759dfb8c30e Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Wed, 4 Nov 2020 10:52:13 -0500 Subject: [PATCH 1/2] Merge::FieldGroupConstant --- Gemfile.lock | 2 +- lib/kiba/extend/transforms/merge.rb | 32 ++++++++++++++++++- lib/kiba/extend/version.rb | 2 +- spec/kiba/extend/transforms/merge_spec.rb | 38 +++++++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a28490a01..33e3843df 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - kiba-extend (1.4.0) + kiba-extend (1.4.1) 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..8afb77488 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 FieldGroupConstant + 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..dec1eb3fc 100644 --- a/spec/kiba/extend/transforms/merge_spec.rb +++ b/spec/kiba/extend/transforms/merge_spec.rb @@ -157,6 +157,44 @@ end end + describe 'FieldGroupConstant' 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::FieldGroupConstant, + 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 = [ From be7a1ee289c2b84c00a712a990946920f0e00739 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Wed, 4 Nov 2020 10:54:19 -0500 Subject: [PATCH 2/2] rename to Merge::MultivalueConstant --- Gemfile.lock | 2 +- lib/kiba/extend/transforms/merge.rb | 2 +- spec/kiba/extend/transforms/merge_spec.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 33e3843df..5848aa26b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - kiba-extend (1.4.1) + 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 8afb77488..0a8933847 100644 --- a/lib/kiba/extend/transforms/merge.rb +++ b/lib/kiba/extend/transforms/merge.rb @@ -81,7 +81,7 @@ def process(row) end end - class FieldGroupConstant + class MultivalueConstant def initialize(on_field:, target:, value:, sep:, placeholder:) @on_field = on_field @target = target diff --git a/spec/kiba/extend/transforms/merge_spec.rb b/spec/kiba/extend/transforms/merge_spec.rb index dec1eb3fc..e2cf39347 100644 --- a/spec/kiba/extend/transforms/merge_spec.rb +++ b/spec/kiba/extend/transforms/merge_spec.rb @@ -157,7 +157,7 @@ end end - describe 'FieldGroupConstant' do + describe 'MultivalueConstant' do test_csv = 'tmp/test.csv' rows = [ ['name'], @@ -185,7 +185,7 @@ { name: 'NULL;Earhart', species: 'NULL;guinea fowl' } ] result = execute_job(filename: test_csv, - xform: Merge::FieldGroupConstant, + xform: Merge::MultivalueConstant, xformopt: { on_field: :name, target: :species, value: 'guinea fowl',