diff --git a/Gemfile.lock b/Gemfile.lock index c73da93a2..d9667e4a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - kiba-extend (1.2.1) + kiba-extend (1.3.0) activesupport kiba (>= 3.0.0) kiba-common (>= 0.9.0) diff --git a/lib/kiba/extend/transforms/replace.rb b/lib/kiba/extend/transforms/replace.rb index 61ad19143..eb885249d 100644 --- a/lib/kiba/extend/transforms/replace.rb +++ b/lib/kiba/extend/transforms/replace.rb @@ -3,6 +3,22 @@ module Extend module Transforms module Replace ::Replace = Kiba::Extend::Transforms::Replace + class EmptyFieldValues + def initialize(fields:, value:) + @fields = fields + @value = value + end + + def process(row) + blankfields = @fields.map{ |field| [field, row.fetch(field, nil)] } + .select{ |pair| pair[1].blank? } + .map{ |pair| pair[0] } + return row if blankfields.empty? + blankfields.each{ |field| row[field] = @value } + row + end + end + class FieldValueWithStaticMapping def initialize(source:, target:, mapping:, fallback_val: :orig, delete_source: true, multival: false, sep: '') diff --git a/lib/kiba/extend/version.rb b/lib/kiba/extend/version.rb index a53928e15..b0793aeda 100644 --- a/lib/kiba/extend/version.rb +++ b/lib/kiba/extend/version.rb @@ -1,5 +1,5 @@ module Kiba module Extend - VERSION = "1.3.0" + VERSION = "1.4.0" end end diff --git a/spec/kiba/extend/transforms/replace_spec.rb b/spec/kiba/extend/transforms/replace_spec.rb index 4088cb1b4..2d8fef244 100644 --- a/spec/kiba/extend/transforms/replace_spec.rb +++ b/spec/kiba/extend/transforms/replace_spec.rb @@ -1,7 +1,30 @@ require 'spec_helper' RSpec.describe Kiba::Extend::Transforms::Replace do - describe 'FieldValueWithStaticMapping' do + + describe 'EmptyFieldValues' do + test_csv = 'tmp/test.csv' + rows = [ + ['id', 'species', 'name', 'sex'], + [1, 'guineafowl', nil, ''], + ] + + before do + generate_csv(test_csv, rows) + end + it 'replaces empty field values in specified field(s) with given string' do + expected = [ + {id: '1', species: 'guineafowl', name: 'NULL', sex: 'NULL'} + ] + result = execute_job(filename: test_csv, + xform: Replace::EmptyFieldValues, + xformopt: {fields: %i[name sex], + value: 'NULL'}) + expect(result).to eq(expected) + end + end + + describe 'FieldValueWithStaticMapping' do test_csv = 'tmp/test.csv' mapping = { 'm' => 'male',