Class: Kiba::Extend::Transforms::Delete::FieldnamesStartingWith

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/delete/fieldnames_starting_with.rb

Overview

Deletes field(s) whose names begin with the given prefix string

Examples:

# Used in pipeline as:
# transform Delete::FieldnamesStartingWith,
#   prefix: "fp_"
xform = Delete::FieldnamesStartingWith.new(
  prefix: "fp_"
)
input = [
  {a: 'ant', b: 'bee', c: nil, d: 'deer', e: nil,
   fp: 'YmVlOzs7bmlsOzs7ZGVlcjs7O2VtcHR5',
   fp_b: 'bee', fp_c: nil, fp_d: 'deer', fp_e: '',
   changed: nil},
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {a: 'ant', b: 'bee', c: nil, d: 'deer', e: nil,
   fp: 'YmVlOzs7bmlsOzs7ZGVlcjs7O2VtcHR5',
   changed: nil},
]
expect(result).to eq(expected)

Since:

  • 4.0.0

Instance Method Summary collapse

Constructor Details

#initialize(prefix:) ⇒ FieldnamesStartingWith

Returns a new instance of FieldnamesStartingWith.

Parameters:

  • prefix (String)

    if a fieldname begins with or equals this string, the field will be deleted

Since:

  • 4.0.0



35
36
37
# File 'lib/kiba/extend/transforms/delete/fieldnames_starting_with.rb', line 35

def initialize(prefix:)
  @prefix = prefix
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 4.0.0



40
41
42
43
44
45
46
47
48
# File 'lib/kiba/extend/transforms/delete/fieldnames_starting_with.rb', line 40

def process(row)
  row.keys.each do |field|
    next unless field.to_s.start_with?(prefix)

    row.delete(field)
  end

  row
end