Class: Kiba::Extend::Transforms::Prepend::ToFieldValue

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/prepend/to_field_value.rb

Overview

Adds the specified value to the specified field

If target field value is blank, it is left blank

Examples:

Treated as single value (default)

# Used in pipeline as:
# transform Prepend::ToFieldValue, field: :name, value: 'aka: '

xform = Prepend::ToFieldValue.new(field: :name, value: 'aka: ')
input = [
    {name: 'Weddy'},
    {name: 'Kernel|Zipper'},
    {name: nil},
    {name: ''}
  ]
result = input.map{ |row| xform.process(row) }
expected = [
    {name: 'aka: Weddy'},
    {name: 'aka: Kernel|Zipper'},
    {name: nil},
    {name: ''}
  ]
expect(result).to eq(expected)

Treated as multivalue

# Used in pipeline as:
# transform Prepend::ToFieldValue, field: :name, value: 'aka: ', multival: true, delim: '|'

xform = Prepend::ToFieldValue.new(field: :name, value: 'aka: ', multival: true, delim: '|')
input = [
    {name: 'Weddy'},
    {name: 'Kernel|Zipper'},
    {name: nil},
    {name: ''}
  ]
result = input.map{ |row| xform.process(row) }
expected = [
    {name: 'aka: Weddy'},
    {name: 'aka: Kernel|aka: Zipper'},
    {name: nil},
    {name: ''}
  ]
expect(result).to eq(expected)

Instance Method Summary collapse

Constructor Details

#initialize(field:, value:, multival: false, delim: Kiba::Extend.delim) ⇒ ToFieldValue

Note:

mvdelim argument is deprecated and replaced by multival and delim

Returns a new instance of ToFieldValue.

Parameters:

  • field (Symbol)

    The field to prepend to

  • value (String)

    The value to be prepended

  • multival (Boolean) (defaults to: false)

    Whether prepend to multiple values

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

    for splitting value if multival



58
59
60
61
62
63
64
# File 'lib/kiba/extend/transforms/prepend/to_field_value.rb', line 58

def initialize(field:, value:, multival: false,
  delim: Kiba::Extend.delim)
  @field = field
  @value = value
  @multival = multival
  @delim = delim
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


67
68
69
70
71
72
73
74
75
76
# File 'lib/kiba/extend/transforms/prepend/to_field_value.rb', line 67

def process(row)
  fieldval = row.fetch(field, nil)
  return row if fieldval.blank?

  fieldvals = multival ? fieldval.split(delim) : [fieldval]
  row[field] = fieldvals.map do |fieldval|
    "#{value}#{fieldval}"
  end.join(delim)
  row
end