Class: Kiba::Extend::Transforms::StringValue::ToArray

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/string_value/to_array.rb

Overview

Splits given field value into an array on given delimiter. Wraps field value in an array if delim is explicitly set to nil.

Examples:

Delimiter is provided

# Used in pipeline as:
# transform StringValue::ToArray, fields: :r1, delim: ';'
xform = StringValue::ToArray.new(fields: :r1, delim: ';')
input = [
  {r1: 'a;b', r2: 'foo;bar'}
]
result = input.map{|row| xform.process(row)}
expected = [
  {r1: ['a','b'], r2: 'foo;bar'}
]
expect(result).to eq(expected)

Delimiter is not provided

# Used in pipeline as:
# transform StringValue::ToArray, fields: :r1
xform = StringValue::ToArray.new(fields: :r1)
input = [
  {r1: 'a|b', r2: 'foo|bar'}
]
result = input.map{|row| xform.process(row)}
expected = [
  {r1: ['a','b'], r2: 'foo|bar'}
]
expect(result).to eq(expected)

Delimiter is nil

# Used in pipeline as:
# transform StringValue::ToArray, fields: :r1, delim: nil
xform = StringValue::ToArray.new(fields: :r1, delim: nil)
input = [
  {r1: 'a;b', r2: 'foo;bar'}
]
result = input.map{|row| xform.process(row)}
expected = [
  {r1: ['a;b'], r2: 'foo;bar'}
]
expect(result).to eq(expected)

Mulitple fields using default delimiter

# Used in pipeline as:
# transform StringValue::ToArray, fields: %i[r1 r2]
xform = StringValue::ToArray.new(fields: [:r1,:r2])
input = [
  {r1: 'a|b', r2: 'foo|bar'}
]
result = input.map{|row| xform.process(row)}
expected = [
  {r1: ['a','b'], r2: ['foo','bar']}
]
expect(result).to eq(expected)

Empty array when fieldval is nil and delim is nil

# Used in pipeline as:
# transform StringValue::ToArray, fields: :r1, delim: nil
xform = StringValue::ToArray.new(fields: :r1, delim: nil)
input = [
  {r1: nil, r2: 'foo;bar'}
]
result = input.map{|row| xform.process(row)}
expected = [
  {r1: [], r2: 'foo;bar'}
]
expect(result).to eq(expected)

Empty array when fieldval is nil

# Used in pipeline as:
# transform StringValue::ToArray, fields: :r1
xform = StringValue::ToArray.new(fields: :r1)
input = [
  {r1: nil, r2: 'foo;bar'}
]
result = input.map{|row| xform.process(row)}
expected = [
  {r1: [], r2: 'foo;bar'}
]
expect(result).to eq(expected)

Since:

  • 3.3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields:, delim: Kiba::Extend.delim) ⇒ ToArray

Returns a new instance of ToArray.

Parameters:

  • fields (Symbol, Array(Symbol))

    Source data fields.

  • delim (String, nil) (defaults to: Kiba::Extend.delim)

    The delimiting character. If no delim is given, the default delim is Kiba::Extend.delim. If nil is provided (do not delimit string value), the string value will instead be wrapped in an array without attempting to split the string value. If nil is provided for delim and a field’s value is nil, this will create an empty array.

Since:

  • 3.3.0



97
98
99
100
# File 'lib/kiba/extend/transforms/string_value/to_array.rb', line 97

def initialize(fields:, delim: Kiba::Extend.delim)
  @fields = [fields].flatten
  @delim = delim
end

Instance Attribute Details

#delimObject (readonly)

Since:

  • 3.3.0



115
116
117
# File 'lib/kiba/extend/transforms/string_value/to_array.rb', line 115

def delim
  @delim
end

#fieldsObject (readonly)

Since:

  • 3.3.0



115
116
117
# File 'lib/kiba/extend/transforms/string_value/to_array.rb', line 115

def fields
  @fields
end

Instance Method Details

#process(row) ⇒ Object

Since:

  • 3.3.0



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/kiba/extend/transforms/string_value/to_array.rb', line 102

def process(row)
  fields.each do |field|
    fieldval = row[field]
    row[field] = if delim.nil?
      fieldval.nil? ? [] : [fieldval]
    else
      fieldval.nil? ? [] : fieldval.split(delim)
    end
  end

  row
end