Skip to content

Commit

Permalink
Merge pull request #4 from lyrasis/delete-source-fix
Browse files Browse the repository at this point in the history
Delete source fix
  • Loading branch information
kspurgin authored Nov 2, 2020
2 parents 7303932 + 42e91a2 commit 1394a29
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 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.2.0)
kiba-extend (1.2.1)
activesupport
kiba (>= 3.0.0)
kiba-common (>= 0.9.0)
Expand Down
1 change: 1 addition & 0 deletions lib/kiba/extend/transforms/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def initialize(fields:, find:, replace:, casesensitive: true, multival: false, s
end

def process(row)
@fields = @fields == :all ? row.keys : @fields
@fields.each do |field|
oldval = row.fetch(field)
if oldval.nil?
Expand Down
29 changes: 19 additions & 10 deletions lib/kiba/extend/transforms/replace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,37 @@ module Transforms
module Replace
::Replace = Kiba::Extend::Transforms::Replace
class FieldValueWithStaticMapping
def initialize(source:, target:, mapping:, fallback_val: :orig, delete_source: true)
def initialize(source:, target:, mapping:, fallback_val: :orig, delete_source: true,
multival: false, sep: '')
@source = source
@target = target
@mapping = mapping
@mapping[nil] = nil unless @mapping.has_key?(nil)
@fallback = fallback_val
@del = delete_source
@multival = multival
@sep = sep
end

def process(row)
origval = row.fetch(@source)
if @mapping.has_key?(origval)
row[@target] = @mapping[origval]
else
case @fallback
origval = @multival ? row.fetch(@source).split(@sep) : [row.fetch(@source)]
newvals = []

origval.each do |oval|
if @mapping.has_key?(oval)
newvals << @mapping[oval]
else
case @fallback
when :orig
row[@target] = origval
when :nil
row[@target] = nil
newvals << oval
when :nil
newvals << nil
end
end
end
row.delete(@source) if @del

row[@target] = newvals.length > 1 ? newvals.join(@sep) : newvals.first
row.delete(@source) if @del unless @source == @target
row
end
end
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.2.1"
VERSION = "1.3.0"
end
end
12 changes: 8 additions & 4 deletions spec/kiba/extend/transforms/replace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@
rows = [
['id', 'name', 'sex'],
[1, 'Weddy', 'm'],
[2, 'Kernel', 'f']
[2, 'Kernel', 'f'],
[3, 'Earlybird;Earhart', 'f;f']
]

before do
generate_csv(test_csv, rows)
end
it 'adds field value from static mapping' do
expected = [
{:id=>'1', :name=>'Weddy', :gender=>'male'},
{:id=>'2', :name=>'Kernel', :gender=>'female'}
{id: '1', name: 'Weddy', gender: 'male'},
{id: '2', name: 'Kernel', gender: 'female'},
{id: '3', name: 'Earlybird;Earhart', gender: 'female;female'}
]
result = execute_job(filename: test_csv,
xform: Replace::FieldValueWithStaticMapping,
xformopt: {source: :sex,
target: :gender,
mapping: mapping})
mapping: mapping,
multival: true,
sep: ';'})
expect(result).to eq(expected)
end
context 'When mapping does not contain matching key' do
Expand Down

0 comments on commit 1394a29

Please sign in to comment.