Class: Kiba::Extend::Transforms::Explode::RowsFromMultivalField

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/explode/rows_from_multival_field.rb

Overview

Splits given field value on given delimiter. Original row is removed. One new row per split value is added. Value of split field is one of the split values per row. All other values in row are left the same

Examples:

With defaults

# Used in pipeline as:
# transform Explode::RowsFromMultivalField,
#   field: :r1
xform = Explode::RowsFromMultivalField.new(
  field: :r1
)
input = [
  {r1: "a|b|c", r2: "c|d"},
  {r1: "", r2: "e|f"},
  {r1: nil, r2: "g|h"},
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {r1: "a", r2: "c|d"},
  {r1: "b", r2: "c|d"},
  {r1: "c", r2: "c|d"},
  {r1: "", r2: "e|f"},
  {r1: nil, r2: "g|h"},
]
expect(result).to eq(expected)

With delim given

# Used in pipeline as:
# transform Explode::RowsFromMultivalField,
#   field: :r1, delim: "^^"
xform = Explode::RowsFromMultivalField.new(
  field: :r1, delim: "^^"
)
input = [
  {r1: "a^^b", r2: "c^^d"},
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {r1: "a", r2: "c^^d"},
  {r1: "b", r2: "c^^d"},
]
expect(result).to eq(expected)

Instance Method Summary collapse

Constructor Details

#initialize(field:, delim: nil) ⇒ RowsFromMultivalField

Returns a new instance of RowsFromMultivalField.

Parameters:

  • field (Symbol)

    the field from which rows will be created

  • delim (String) (defaults to: nil)

    used to split field value. Kiba::Extend.delim used if value not given



55
56
57
58
59
# File 'lib/kiba/extend/transforms/explode/rows_from_multival_field.rb', line 55

def initialize(field:, delim: nil)
  @exploder = RowsFromGroupedMultivalFields.new(
    fields: field, delim: delim
  )
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

  • row (Hash{ Symbol => String, nil })


62
63
64
# File 'lib/kiba/extend/transforms/explode/rows_from_multival_field.rb', line 62

def process(row)
  exploder.process(row) { |r| yield r }
end