From b932c2911ee91ccafbffb7fa12da1654abdfdab5 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Thu, 15 Oct 2020 15:31:52 -0400 Subject: [PATCH 1/2] remove binding.pry --- lib/kiba/extend/transforms/deduplicate.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/kiba/extend/transforms/deduplicate.rb b/lib/kiba/extend/transforms/deduplicate.rb index 8ff497bda..b843c6fbc 100644 --- a/lib/kiba/extend/transforms/deduplicate.rb +++ b/lib/kiba/extend/transforms/deduplicate.rb @@ -42,7 +42,6 @@ def process(row) else targetvals = targetvals.map{ |vals| vals.first } end -# binding.pry targetvals = targetvals.map{ |val| val.blank? ? nil : val } targetvals.each_with_index{ |val, i| row[@targets[i]] = val } From 82bdc47f27c4d082d4ef3ff856487644ce286cd0 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Thu, 15 Oct 2020 15:43:30 -0400 Subject: [PATCH 2/2] add :nulltonil csv converter --- Gemfile.lock | 2 +- lib/kiba/extend.rb | 12 ++++++++++++ lib/kiba/extend/version.rb | 2 +- spec/kiba/extend_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ec3b2b980..0400d02ef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - kiba-extend (1.0.0) + kiba-extend (1.1.0) activesupport kiba (>= 3.0.0) kiba-common (>= 0.9.0) diff --git a/lib/kiba/extend.rb b/lib/kiba/extend.rb index 8a1779673..8ee08982c 100644 --- a/lib/kiba/extend.rb +++ b/lib/kiba/extend.rb @@ -47,5 +47,17 @@ module Extend end } + CSV::Converters[:nulltonil] = lambda{ |s| + begin + if s == 'NULL' + nil + else + s + end + rescue ArgumentError + s + end + } + end end diff --git a/lib/kiba/extend/version.rb b/lib/kiba/extend/version.rb index 89cced63b..10d5ddd11 100644 --- a/lib/kiba/extend/version.rb +++ b/lib/kiba/extend/version.rb @@ -1,5 +1,5 @@ module Kiba module Extend - VERSION = "1.1.0" + VERSION = "1.2.0" end end diff --git a/spec/kiba/extend_spec.rb b/spec/kiba/extend_spec.rb index a27f8a3e6..65164745e 100644 --- a/spec/kiba/extend_spec.rb +++ b/spec/kiba/extend_spec.rb @@ -22,4 +22,26 @@ end after { File.delete(test_csv) if File.exist?(test_csv) } end + + describe ':nulltonil csv converter' do + test_csv = 'tmp/test.csv' + rows = [ + ['id', 'val'], + ['1', 'NULL'], + ['2', 'a NULL value'], + ['3', ' NULL'] + ] + + before { generate_csv(test_csv, rows) } + it 'converts input data' do + expected = [ + { id: '1', val: nil }, + { id: '2', val: 'a NULL value' }, + { id: '3', val: nil } + ] + result = job_csv(filename: test_csv, incsvopt: {converters: [:stripplus, :nulltonil]}) + expect(result).to eq(expected) + end + after { File.delete(test_csv) if File.exist?(test_csv) } + end end