Class: Kiba::Extend::Transforms::Append::NilFields

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/append/nil_fields.rb

Overview

Adds the given field(s) to the row with nil value if they do not already exist in row

Examples:

# Used in pipeline as:
# transform Append::NilFields, fields: %i[a b z]

xform = Append::NilFields.new(fields: %i[a b z])
input = [{z: "zz"}]
result = input.map{ |row| xform.process(row) }
expected = [{z: "zz", a: nil, b: nil}]
expect(result).to eq(expected)

Instance Method Summary collapse

Constructor Details

#initialize(fields:) ⇒ NilFields

Returns a new instance of NilFields.

Parameters:

  • fields (Array<Symbol>, Symbol)

    field name or list of field names to add



22
23
24
# File 'lib/kiba/extend/transforms/append/nil_fields.rb', line 22

def initialize(fields:)
  @fields = [fields].flatten
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


27
28
29
30
31
32
# File 'lib/kiba/extend/transforms/append/nil_fields.rb', line 27

def process(row)
  @fields.each do |field|
    row[field] = nil unless row.key?(field)
  end
  row
end