-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>, 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |