From ad9ebefc25078e6991dd2b79a671f68eb3d74e1d Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Mon, 12 Aug 2024 15:53:25 -0400 Subject: [PATCH 1/4] Update rspec version --- Gemfile.lock | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 1de355ae6..9efaee05c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) From 727a7f7012d2d2fcd8c48c97903963a15236396f Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Mon, 12 Aug 2024 16:06:47 -0400 Subject: [PATCH 2/4] Split transforms to different files --- lib/kiba/extend/transforms/append.rb | 92 +------------------ .../extend/transforms/append/nil_fields.rb | 51 ++++++++++ .../transforms/append/to_field_value.rb | 54 +++++++++++ 3 files changed, 106 insertions(+), 91 deletions(-) create mode 100644 lib/kiba/extend/transforms/append/nil_fields.rb create mode 100644 lib/kiba/extend/transforms/append/to_field_value.rb diff --git a/lib/kiba/extend/transforms/append.rb b/lib/kiba/extend/transforms/append.rb index 66e8c2440..71ca52bfc 100644 --- a/lib/kiba/extend/transforms/append.rb +++ b/lib/kiba/extend/transforms/append.rb @@ -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] 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 diff --git a/lib/kiba/extend/transforms/append/nil_fields.rb b/lib/kiba/extend/transforms/append/nil_fields.rb new file mode 100644 index 000000000..f246d7af6 --- /dev/null +++ b/lib/kiba/extend/transforms/append/nil_fields.rb @@ -0,0 +1,51 @@ +# 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 + # + # # 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] 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 diff --git a/lib/kiba/extend/transforms/append/to_field_value.rb b/lib/kiba/extend/transforms/append/to_field_value.rb new file mode 100644 index 000000000..502bbbec7 --- /dev/null +++ b/lib/kiba/extend/transforms/append/to_field_value.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module Kiba + module Extend + module Transforms + module Append + # # 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 +end From 36566e4da6cfcb7593698e5d5c1833840923fa6c Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Mon, 12 Aug 2024 16:14:09 -0400 Subject: [PATCH 3/4] Switch to yardspec tests --- .../extend/transforms/append/nil_fields.rb | 30 +++------- .../transforms/append/to_field_value.rb | 45 +++++++------- spec/kiba/extend/transforms/append_spec.rb | 58 ------------------- 3 files changed, 28 insertions(+), 105 deletions(-) delete mode 100644 spec/kiba/extend/transforms/append_spec.rb diff --git a/lib/kiba/extend/transforms/append/nil_fields.rb b/lib/kiba/extend/transforms/append/nil_fields.rb index f246d7af6..0ed3dc04a 100644 --- a/lib/kiba/extend/transforms/append/nil_fields.rb +++ b/lib/kiba/extend/transforms/append/nil_fields.rb @@ -7,29 +7,15 @@ module Append # Adds the given field(s) to the row with nil value if they do not # already exist in row # - # # Examples + # @example + # # Used in pipeline as: + # # transform Append::NilFields, fields: %i[a b z] # - # 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 | - # ~~~ + # 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] field name or list of field # names to add diff --git a/lib/kiba/extend/transforms/append/to_field_value.rb b/lib/kiba/extend/transforms/append/to_field_value.rb index 502bbbec7..2ede43cf5 100644 --- a/lib/kiba/extend/transforms/append/to_field_value.rb +++ b/lib/kiba/extend/transforms/append/to_field_value.rb @@ -4,33 +4,28 @@ module Kiba module Extend module Transforms module Append - # # Examples + # Adds the given value to the end of value of the given field. Does not + # affect nil/empty field values # - # Input table: + # @example Treated as single value (default) + # # Used in pipeline as: + # # transform Append::ToFieldValue, field: :name, value: " (name)" # - # ~~~ - # | name | - # |-------| - # | Weddy | - # | nil | - # | | - # ~~~ - # - # Used in pipeline as: - # - # ~~~ - # transform Append::ToFieldValue, field: :name, value: ' (name)' - # ~~~ - # - # Results in: - # - # ~~~ - # | name | - # |--------------| - # | Weddy (name) | - # | nil | - # | | - # ~~~ + # 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) class ToFieldValue # @param field [Symbol] name of field to append to # @param value [String] value to append to existing field values diff --git a/spec/kiba/extend/transforms/append_spec.rb b/spec/kiba/extend/transforms/append_spec.rb deleted file mode 100644 index ecbd5375d..000000000 --- a/spec/kiba/extend/transforms/append_spec.rb +++ /dev/null @@ -1,58 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -RSpec.describe Kiba::Extend::Transforms::Append do - let(:accumulator) { [] } - let(:test_job) do - Helpers::TestJob.new(input: input, accumulator: accumulator, - transforms: transforms) - end - let(:result) { test_job.accumulator } - - describe "NilFields" do - let(:input) { [{z: "zz"}] } - - let(:transforms) do - Kiba.job_segment do - transform Append::NilFields, fields: %i[a b c z] - end - end - - let(:expected) { [{z: "zz", a: nil, b: nil, c: nil}] } - - it "adds non-existing fields, populating with nil, "\ - "while leaving existing fields alone" do - expect(result).to eq(expected) - end - end - - describe "ToFieldValue" do - let(:input) do - [ - {name: "Weddy"}, - {name: nil}, - {name: ""} - ] - end - - let(:transforms) do - Kiba.job_segment do - transform Append::ToFieldValue, field: :name, value: " (name)" - end - end - - let(:expected) do - [ - {name: "Weddy (name)"}, - {name: nil}, - {name: ""} - ] - end - - it "prepends given value to existing field values, "\ - "leaving blank values alone" do - expect(result).to eq(expected) - end - end -end From 46d4fa4708012e52cf6f211e1f1c1188d3b4eeed Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Mon, 12 Aug 2024 16:19:34 -0400 Subject: [PATCH 4/4] Add delim param to trigger multival treatment --- CHANGELOG.adoc | 1 + .../transforms/append/to_field_value.rb | 33 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index ccf5dafc6..21a89e409 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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 diff --git a/lib/kiba/extend/transforms/append/to_field_value.rb b/lib/kiba/extend/transforms/append/to_field_value.rb index 2ede43cf5..210e7f6f8 100644 --- a/lib/kiba/extend/transforms/append/to_field_value.rb +++ b/lib/kiba/extend/transforms/append/to_field_value.rb @@ -26,12 +26,39 @@ 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 }] @@ -39,7 +66,9 @@ 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