From 5f461f65524af8767e1678367434fe6f4a36da27 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Thu, 7 Jan 2021 16:45:21 -0500 Subject: [PATCH 1/3] stripplus CSV converter strips leading/trailing %LINEBREAK% and %CRLF% --- lib/kiba/extend.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/kiba/extend.rb b/lib/kiba/extend.rb index 4d7fd9839..57b1d5863 100644 --- a/lib/kiba/extend.rb +++ b/lib/kiba/extend.rb @@ -41,6 +41,8 @@ module Extend s.strip .gsub(/ +/, ' ') .sub(/,$/, '') + .sub(/^%(LINEBREAK|CRLF)%/, '') + .sub(/%(LINEBREAK|CRLF)%$/, '') .strip end rescue ArgumentError From 0ba9448c8dfffcce5375e6d0df70020d18b4cfb5 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Thu, 18 Feb 2021 13:01:41 -0500 Subject: [PATCH 2/3] cells = NULL are transformed to nil by both csv converters --- lib/kiba/extend.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/kiba/extend.rb b/lib/kiba/extend.rb index 57b1d5863..ba849eb75 100644 --- a/lib/kiba/extend.rb +++ b/lib/kiba/extend.rb @@ -37,6 +37,8 @@ module Extend begin if s.nil? nil + elsif s == 'NULL' + nil else s.strip .gsub(/ +/, ' ') From 56651faa1cc865178c3808ecccdd39902024ce7b Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Thu, 18 Feb 2021 13:02:10 -0500 Subject: [PATCH 3/3] add optional `if_equals` argument to Clean::ClearFields --- lib/kiba/extend/transforms/clean.rb | 11 ++++++++-- spec/kiba/extend/transforms/clean_spec.rb | 25 +++++++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/kiba/extend/transforms/clean.rb b/lib/kiba/extend/transforms/clean.rb index d8f7ee21a..e5330dc95 100644 --- a/lib/kiba/extend/transforms/clean.rb +++ b/lib/kiba/extend/transforms/clean.rb @@ -34,12 +34,19 @@ def process(row) end class ClearFields - def initialize(fields:) + def initialize(fields:, if_equals: nil) @fields = fields + @if_equals = if_equals end def process(row) - @fields.each{ |field| row[field] = nil } + @fields.each do |field| + if @if_equals.nil? + row[field] = nil + else + row[field] = nil if row[field] == @if_equals + end + end row end end diff --git a/spec/kiba/extend/transforms/clean_spec.rb b/spec/kiba/extend/transforms/clean_spec.rb index 32f841693..1773303d5 100644 --- a/spec/kiba/extend/transforms/clean_spec.rb +++ b/spec/kiba/extend/transforms/clean_spec.rb @@ -42,13 +42,30 @@ ['5', 'Person;notmapped'] ] - before { generate_csv(test_csv, rows) } - let(:result) { execute_job(filename: test_csv, - xform: Clean::ClearFields, - xformopt: {fields: %i[type]}) } + before { generate_csv(test_csv, rows) } + context 'without additional arguments' do it 'sets the value of the field in all rows to nil' do + result = execute_job(filename: test_csv, + xform: Clean::ClearFields, + xformopt: {fields: %i[type]}) + expect(result.map{ |e| e[1]}.uniq[0]).to be_nil + end + end + context 'with if_equals argument' do + it 'sets the value of the field to nil if it matches `if_equals`' do + result = execute_job(filename: test_csv, + xform: Clean::ClearFields, + xformopt: {fields: %i[type], if_equals: ';'}) + expected = [ + {:id=>'1', :type=>'Person;unmapped;Organization'}, + {:id=>'2', :type=>nil}, + {:id=>'3', :type=>nil}, + {:id=>'4', :type=>''}, + {:id=>'5', :type=>'Person;notmapped'}, + ] expect(result.map{ |e| e[1]}.uniq[0]).to be_nil end + end end describe 'DelimiterOnlyFields' do