Skip to content

Commit

Permalink
Merge pull request #15 from lyrasis/append-fields
Browse files Browse the repository at this point in the history
add Append::NilFields transform class
  • Loading branch information
kspurgin authored May 21, 2021
2 parents d408d43 + f822360 commit 1abc820
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
13 changes: 13 additions & 0 deletions lib/kiba/extend/transforms/append.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ module Extend
module Transforms
module Append
::Append = Kiba::Extend::Transforms::Append
class NilFields
def initialize(fields:)
@fields = fields
end

def process(row)
@fields.each do |field|
row[field] = nil unless row.key?(field)
end
row
end
end

class ToFieldValue
def initialize(field:, value:)
@field = field
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.10.0"
VERSION = "1.11.0"
end
end
38 changes: 25 additions & 13 deletions spec/kiba/extend/transforms/append_spec.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
require 'spec_helper'

RSpec.describe Kiba::Extend::Transforms::Append do
let(:test_csv) { 'tmp/test.csv' }
before{ generate_csv(test_csv, rows) }

describe 'NilFields' do
let(:rows) { [
['id', 'z'],
[1, 'zz']
] }
let(:result) { execute_job(filename: test_csv,
xform: Append::NilFields,
xformopt: {fields: %i[a b c z]}) }
it 'adds non-existing fields, populating with nil, while leaving existing fields alone' do
expected = {id: '1', z: 'zz', a: nil, b: nil, c: nil}
expect(result[0]).to eq(expected)
end
end

describe 'ToFieldValue' do
test_csv = 'tmp/test.csv'
rows = [
let(:rows) { [
['id', 'name'],
[1, 'Weddy'],
[2, nil],
[3, '']
]

before do
generate_csv(test_csv, rows)
@result = execute_job(filename: test_csv,
xform: Append::ToFieldValue,
xformopt: {field: :name, value: ' (name)'})
end
] }
let(:result) { execute_job(filename: test_csv,
xform: Append::ToFieldValue,
xformopt: {field: :name, value: ' (name)'}) }
it 'prepends given value to existing field values' do
expected = {id: '1', name: 'Weddy (name)'}
expect(@result[0]).to eq(expected)
expect(result[0]).to eq(expected)
end
it 'leaves nil values alone' do
expected = {id: '2', name: nil}
expect(@result[1]).to eq(expected)
expect(result[1]).to eq(expected)
end
it 'leaves blank values alone' do
expected = {id: '3', name: ''}
expect(@result[2]).to eq(expected)
expect(result[2]).to eq(expected)
end
end
end

0 comments on commit 1abc820

Please sign in to comment.