Skip to content

Commit

Permalink
Merge pull request #18 from lyrasis/merge-compare-fields
Browse files Browse the repository at this point in the history
Add `Merge::CompareFieldsFlag` transform
  • Loading branch information
kspurgin authored Jun 1, 2021
2 parents e39b72c + 26553b5 commit ad2c48b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 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.12.0)
kiba-extend (1.13.0)
activesupport
kiba (>= 4.0.0)
kiba-common (>= 1.5.0)
Expand Down
25 changes: 25 additions & 0 deletions lib/kiba/extend/transforms/merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ module Transforms
module Merge
::Merge = Kiba::Extend::Transforms::Merge

class CompareFieldsFlag
def initialize(fields:, target:, downcase: true, strip: true, ignore_blank: false)
@fields = fields
@target = target
@strip = strip
@downcase = downcase
@ignore_blank = ignore_blank
end

def process(row)
row[@target] = 'diff'
values = []
@fields.each do |field|
value = row.fetch(field, '').dup
value = '' if value.nil?
value.downcase! if @downcase
value.strip! if @strip
values << value
end
values.reject!{ |val| val.blank? } if @ignore_blank
row[@target] = 'same' if values.uniq.length == 1 || values.empty?
row
end
end

class ConstantValue
def initialize(target:, value:)
@target = target
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.12.1"
VERSION = "1.13.0"
end
end
81 changes: 81 additions & 0 deletions spec/kiba/extend/transforms/merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,87 @@
after do
File.delete(test_csv) if File.exist?(test_csv)
end

describe 'CompareFieldsFlag' do
let(:rows) { [
['id', 'pid', 'zid'],
[1, 1, 1],
[2, nil, 2],
['three', 'Three', 'three'],
['three', ' three', 'three'],
[4, 4, 3],
['', nil, '']
] }

context 'with defaults (downcase and strip = true, ignore_blank = false)' do
it 'merges "same" or "diff" into target field after comparing values in row fields' do
expected = [
{id: '1', pid: '1', zid: '1', comp: 'same'},
{id: '2', pid: nil, zid: '2', comp: 'diff'},
{id: 'three', pid: 'Three', zid: 'three', comp: 'same'},
{id: 'three', pid: ' three', zid: 'three', comp: 'same'},
{id: '4', pid: '4', zid: '3', comp: 'diff'},
{id: '', pid: nil, zid: '', comp: 'same'}
]
result = execute_job(filename: test_csv,
xform: Merge::CompareFieldsFlag,
xformopt: {fields: %i[id pid zid], target: :comp})
expect(result).to eq(expected)
end
end

context 'with downcase false' do
it 'merges "same" or "diff" into target field after comparing values in row fields' do
expected = [
{id: '1', pid: '1', zid: '1', comp: 'same'},
{id: '2', pid: nil, zid: '2', comp: 'diff'},
{id: 'three', pid: 'Three', zid: 'three', comp: 'diff'},
{id: 'three', pid: ' three', zid: 'three', comp: 'same'},
{id: '4', pid: '4', zid: '3', comp: 'diff'},
{id: '', pid: nil, zid: '', comp: 'same'}
]
result = execute_job(filename: test_csv,
xform: Merge::CompareFieldsFlag,
xformopt: {fields: %i[id pid zid], target: :comp, downcase: false})
expect(result).to eq(expected)
end
end

context 'with strip false' do
it 'merges "same" or "diff" into target field after comparing values in row fields' do
expected = [
{id: '1', pid: '1', zid: '1', comp: 'same'},
{id: '2', pid: nil, zid: '2', comp: 'diff'},
{id: 'three', pid: 'Three', zid: 'three', comp: 'same'},
{id: 'three', pid: ' three', zid: 'three', comp: 'diff'},
{id: '4', pid: '4', zid: '3', comp: 'diff'},
{id: '', pid: nil, zid: '', comp: 'same'}
]
result = execute_job(filename: test_csv,
xform: Merge::CompareFieldsFlag,
xformopt: {fields: %i[id pid zid], target: :comp, strip: false})
expect(result).to eq(expected)
end
end

context 'with ignore_blank = true' do
it 'merges "same" or "diff" into target field after comparing values in row fields' do
expected = [
{id: '1', pid: '1', zid: '1', comp: 'same'},
{id: '2', pid: nil, zid: '2', comp: 'same'},
{id: 'three', pid: 'Three', zid: 'three', comp: 'same'},
{id: 'three', pid: ' three', zid: 'three', comp: 'same'},
{id: '4', pid: '4', zid: '3', comp: 'diff'},
{id: '', pid: nil, zid: '', comp: 'same'}
]
result = execute_job(filename: test_csv,
xform: Merge::CompareFieldsFlag,
xformopt: {fields: %i[id pid zid], target: :comp, ignore_blank: true})
expect(result).to eq(expected)
end
end
end

describe 'ConstantValue' do
let(:rows) { [
['id', 'name', 'sex', 'source'],
Expand Down

0 comments on commit ad2c48b

Please sign in to comment.