Skip to content

Commit

Permalink
Add delim param to trigger multival treatment
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Aug 12, 2024
1 parent 89ba413 commit 874a448
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ These changes are merged into the `main` branch, but have not been released. Aft
=== Added
* `MARC::LanguageCodeLookup` transform
* Ability to pass `find` argument to `Clean::RegexpFindReplaceFieldVals` as a `Regexp` object. Not sure why this was not the default initial behavior, but here we are! (PR#196)
* Ability to pass `delim` argument to `Append::ToFieldValue` to trigger multi-value treatment (PR#200)

=== Changed

Expand Down
33 changes: 31 additions & 2 deletions lib/kiba/extend/transforms/append/to_field_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,49 @@ module Append
# {name: ""}
# ]
# expect(result).to eq(expected)
#
# @example Treated as multivalue
# # Used in pipeline as:
# # transform Append::ToFieldValue,
# # field: :name,
# # value: " (name)",
# # delim: "|"
#
# xform = Append::ToFieldValue.new(field: :name, value: " (name)",
# delim: "|")
# input = [
# {name: "Weddy"},
# {name: "Kernel|Zipper"},
# {name: nil},
# {name: ""}
# ]
# result = input.map{ |row| xform.process(row) }
# expected = [
# {name: "Weddy (name)"},
# {name: "Kernel (name)|Zipper (name)"},
# {name: nil},
# {name: ""}
# ]
# expect(result).to eq(expected)
class ToFieldValue
# @param field [Symbol] name of field to append to
# @param value [String] value to append to existing field values
def initialize(field:, value:)
# @param delim [String, nil] indicates multivalue delimiter on which
# to split values, if given
def initialize(field:, value:, delim: nil)
@field = field
@value = value
@delim = delim
end

# @param row [Hash{ Symbol => String, nil }]
def process(row)
fv = row.fetch(@field, nil)
return row if fv.blank?

row[@field] = "#{fv}#{@value}"
vals = @delim ? fv.split(@delim) : [fv]
row[@field] = vals.map { |fieldval| "#{fieldval}#{@value}" }
.join(@delim)
row
end
end
Expand Down

0 comments on commit 874a448

Please sign in to comment.