Class: Kiba::Extend::Transforms::Clean::StripFields

Inherits:
Object
  • Object
show all
Includes:
Allable
Defined in:
lib/kiba/extend/transforms/clean/strip_fields.rb

Overview

Removes all leading/trailing spaces from values in the specified fields.

Examples:

Basic match(default)

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

Multivalue mode (i.e. with delim)

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

Instance Method Summary collapse

Constructor Details

#initialize(fields:, delim: nil) ⇒ StripFields

Returns a new instance of StripFields.

Parameters:

  • fields (Array<Symbol>, Symbol, :all, nil)

    in which to strip values

  • delim (nil, String) (defaults to: nil)

    if given, switches to multivalue mode, in which each value in a multivalued string is stripped



61
62
63
64
# File 'lib/kiba/extend/transforms/clean/strip_fields.rb', line 61

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

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


67
68
69
70
71
72
# File 'lib/kiba/extend/transforms/clean/strip_fields.rb', line 67

def process(row)
  finalize_fields(row)
  fields.each { |field| strip_field(field, row) }

  row
end