Skip to content

Commit

Permalink
Merge pull request #7 from lyrasis/fieldgroup-constant
Browse files Browse the repository at this point in the history
Merge::MultivalueConstant
  • Loading branch information
kspurgin authored Nov 4, 2020
2 parents a455f95 + be7a1ee commit 51a0ab6
Show file tree
Hide file tree
Showing 4 changed files with 71 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.4.0)
kiba-extend (1.5.0)
activesupport
kiba (>= 3.0.0)
kiba-common (>= 0.9.0)
Expand Down
32 changes: 31 additions & 1 deletion lib/kiba/extend/transforms/merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,37 @@ def process(row)
end
row
end
end
end

class MultivalueConstant
def initialize(on_field:, target:, value:, sep:, placeholder:)
@on_field = on_field
@target = target
@value = value
@sep = sep
@placeholder = placeholder
end

def process(row)
field_val = row.fetch(@on_field, nil)
if field_val.blank?
row[@target] = @placeholder
return row
end

merge_vals = []
field_val.split(@sep, -1).each do |field_val|
if field_val == @placeholder || field_val.blank?
merge_vals << @placeholder
else
merge_vals << @value
end
end

row[@target] = merge_vals.join(@sep)
row
end
end

# used when lookup may return an array of rows from which values should be merged
# into the target, AND THE TARGET IS MULTIVALUED
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.4.1"
VERSION = "1.5.0"
end
end
38 changes: 38 additions & 0 deletions spec/kiba/extend/transforms/merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,44 @@
end
end

describe 'MultivalueConstant' do
test_csv = 'tmp/test.csv'
rows = [
['name'],
['Weddy'],
['NULL'],
[''],
[nil],
['Earlybird;Divebomber'],
[';Niblet'],
['Hunter;'],
['NULL;Earhart']
]
before do
generate_csv(test_csv, rows)
end
it 'adds specified value to new field once per value in specified field' do
expected = [
{ name: 'Weddy', species: 'guinea fowl' },
{ name: 'NULL', species: 'NULL' },
{ name: '', species: 'NULL' },
{ name: nil, species: 'NULL' },
{ name: 'Earlybird;Divebomber', species: 'guinea fowl;guinea fowl' },
{ name: ';Niblet', species: 'NULL;guinea fowl' },
{ name: 'Hunter;', species: 'guinea fowl;NULL' },
{ name: 'NULL;Earhart', species: 'NULL;guinea fowl' }
]
result = execute_job(filename: test_csv,
xform: Merge::MultivalueConstant,
xformopt: { on_field: :name,
target: :species,
value: 'guinea fowl',
sep: ';',
placeholder: 'NULL'})
expect(result).to eq(expected)
end
end

describe 'MultiRowLookup' do
test_csv = 'tmp/test.csv'
rows = [
Expand Down

0 comments on commit 51a0ab6

Please sign in to comment.