Skip to content

Commit

Permalink
add optional if_equals argument to Clean::ClearFields
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Feb 18, 2021
1 parent 0ba9448 commit 56651fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
11 changes: 9 additions & 2 deletions lib/kiba/extend/transforms/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions spec/kiba/extend/transforms/clean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 56651fa

Please sign in to comment.