Skip to content

Commit

Permalink
Merge pull request #5 from lyrasis/hold-multi-position
Browse files Browse the repository at this point in the history
adds Replace::EmptyFieldValues
  • Loading branch information
kspurgin authored Nov 2, 2020
2 parents 1394a29 + fa418b2 commit 6206612
Show file tree
Hide file tree
Showing 4 changed files with 42 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.2.1)
kiba-extend (1.3.0)
activesupport
kiba (>= 3.0.0)
kiba-common (>= 0.9.0)
Expand Down
16 changes: 16 additions & 0 deletions lib/kiba/extend/transforms/replace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ module Extend
module Transforms
module Replace
::Replace = Kiba::Extend::Transforms::Replace
class EmptyFieldValues
def initialize(fields:, value:)
@fields = fields
@value = value
end

def process(row)
blankfields = @fields.map{ |field| [field, row.fetch(field, nil)] }
.select{ |pair| pair[1].blank? }
.map{ |pair| pair[0] }
return row if blankfields.empty?
blankfields.each{ |field| row[field] = @value }
row
end
end

class FieldValueWithStaticMapping
def initialize(source:, target:, mapping:, fallback_val: :orig, delete_source: true,
multival: false, sep: '')
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.3.0"
VERSION = "1.4.0"
end
end
25 changes: 24 additions & 1 deletion spec/kiba/extend/transforms/replace_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
require 'spec_helper'

RSpec.describe Kiba::Extend::Transforms::Replace do
describe 'FieldValueWithStaticMapping' do

describe 'EmptyFieldValues' do
test_csv = 'tmp/test.csv'
rows = [
['id', 'species', 'name', 'sex'],
[1, 'guineafowl', nil, ''],
]

before do
generate_csv(test_csv, rows)
end
it 'replaces empty field values in specified field(s) with given string' do
expected = [
{id: '1', species: 'guineafowl', name: 'NULL', sex: 'NULL'}
]
result = execute_job(filename: test_csv,
xform: Replace::EmptyFieldValues,
xformopt: {fields: %i[name sex],
value: 'NULL'})
expect(result).to eq(expected)
end
end

describe 'FieldValueWithStaticMapping' do
test_csv = 'tmp/test.csv'
mapping = {
'm' => 'male',
Expand Down

0 comments on commit 6206612

Please sign in to comment.