Skip to content

Commit

Permalink
Split transforms to different files
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Aug 12, 2024
1 parent 310ce6a commit 9b10ca1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 91 deletions.
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
51 changes: 51 additions & 0 deletions lib/kiba/extend/transforms/append/nil_fields.rb
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
54 changes: 54 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,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

0 comments on commit 9b10ca1

Please sign in to comment.