Skip to content

Commit

Permalink
Merge pull request #2 from lyrasis/csv-converter-nulltonil
Browse files Browse the repository at this point in the history
Csv converter nulltonil
  • Loading branch information
kspurgin authored Oct 15, 2020
2 parents 2a3ad5f + 82bdc47 commit d3d9c26
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
12 changes: 12 additions & 0 deletions lib/kiba/extend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion lib/kiba/extend/transforms/deduplicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion lib/kiba/extend/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Kiba
module Extend
VERSION = "1.1.0"
VERSION = "1.2.0"
end
end
22 changes: 22 additions & 0 deletions spec/kiba/extend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d3d9c26

Please sign in to comment.