Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multivalue treatment to Append::ToFieldValue via delim parameter #200

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 12 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.2.2)
csv (3.2.6)
diff-lcs (1.5.0)
diff-lcs (1.5.1)
docile (1.4.0)
dry-configurable (0.15.0)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -81,19 +81,19 @@ GEM
rake (13.0.1)
regexp_parser (2.8.1)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.56.1)
base64 (~> 0.1.1)
json (~> 2.3)
Expand Down
92 changes: 1 addition & 91 deletions lib/kiba/extend/transforms/append.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,99 +3,9 @@
module Kiba
module Extend
module Transforms
# Adds values to the end of fields or rows
# Namespace for transforms that add values to the end of fields or rows
module Append
::Append = Kiba::Extend::Transforms::Append

# Adds the given field(s) to the row with nil value if they do not
# already exist in row
#
# # Examples
#
# Input table:
#
# ~~~
# | z |
# |----|
# | zz |
# ~~~
#
# Used in pipeline as:
#
# ~~~
# transform Append::NilFields, fields: %i[a b c z]
# ~~~
#
# Results in:
#
# ~~~
# | z | a | b | c |
# |----+-----+-----+-----|
# | zz | nil | nil | nil |
# ~~~
class NilFields
# @param fields [Array<Symbol>, Symbol] field name or list of field
# names to add
def initialize(fields:)
@fields = [fields].flatten
end

# @param row [Hash{ Symbol => String, nil }]
def process(row)
@fields.each do |field|
row[field] = nil unless row.key?(field)
end
row
end
end

# Adds the given value to the end of value of the given field. Does not
# affect nil/empty field values
#
# # Examples
#
# Input table:
#
# ~~~
# | name |
# |-------|
# | Weddy |
# | nil |
# | |
# ~~~
#
# Used in pipeline as:
#
# ~~~
# transform Append::ToFieldValue, field: :name, value: ' (name)'
# ~~~
#
# Results in:
#
# ~~~
# | name |
# |--------------|
# | Weddy (name) |
# | nil |
# | |
# ~~~
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:)
@field = field
@value = value
end

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

row[@field] = "#{fv}#{@value}"
row
end
end
end
end
end
Expand Down
37 changes: 37 additions & 0 deletions lib/kiba/extend/transforms/append/nil_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Kiba
module Extend
module Transforms
module Append
# Adds the given field(s) to the row with nil value if they do not
# already exist in row
#
# @example
# # Used in pipeline as:
# # transform Append::NilFields, fields: %i[a b z]
#
# xform = Append::NilFields.new(fields: %i[a b z])
# input = [{z: "zz"}]
# result = input.map{ |row| xform.process(row) }
# expected = [{z: "zz", a: nil, b: nil}]
# expect(result).to eq(expected)
class NilFields
# @param fields [Array<Symbol>, Symbol] field name or list of field
# names to add
def initialize(fields:)
@fields = [fields].flatten
end

# @param row [Hash{ Symbol => String, nil }]
def process(row)
@fields.each do |field|
row[field] = nil unless row.key?(field)
end
row
end
end
end
end
end
end
78 changes: 78 additions & 0 deletions lib/kiba/extend/transforms/append/to_field_value.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

module Kiba
module Extend
module Transforms
module Append
# Adds the given value to the end of value of the given field. Does not
# affect nil/empty field values
#
# @example Treated as single value (default)
# # Used in pipeline as:
# # transform Append::ToFieldValue, field: :name, value: " (name)"
#
# xform = Append::ToFieldValue.new(field: :name, value: " (name)")
# input = [
# {name: "Weddy"},
# {name: "Kernel|Zipper"},
# {name: nil},
# {name: ""}
# ]
# result = input.map{ |row| xform.process(row) }
# expected = [
# {name: "Weddy (name)"},
# {name: "Kernel|Zipper (name)"},
# {name: nil},
# {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
# @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?

vals = @delim ? fv.split(@delim) : [fv]
row[@field] = vals.map { |fieldval| "#{fieldval}#{@value}" }
.join(@delim)
row
end
end
end
end
end
end
58 changes: 0 additions & 58 deletions spec/kiba/extend/transforms/append_spec.rb

This file was deleted.

Loading