Class: Kiba::Extend::Transforms::FilterRows::FieldEqualTo

Inherits:
Object
  • Object
show all
Includes:
ActionArgumentable
Defined in:
lib/kiba/extend/transforms/filter_rows/field_equal_to.rb

Overview

Keep or reject rows where the value of the specified field exactly matches the given value

Examples

Source data:

{val:  'N'},
{val:  'n'},
{val:  'NN'},
{val:  'NY'},
{val:  ''},
{val:  nil}

Used in pipeline as:

transform FilterRows::FieldEqualTo, action: :keep, field: :val, value: 'N'

Resulting data:

{val:  'N'}

Used in pipeline as:

transform FilterRows::FieldEqualTo, action: :reject, field: :val, value: 'N'

Resulting data:

{val:  'n'},
{val:  'NN'},
{val:  'NY'},
{val:  ''},
{val:  nil}

Instance Method Summary collapse

Constructor Details

#initialize(action:, field:, value:) ⇒ FieldEqualTo

Returns a new instance of FieldEqualTo.

Parameters:

  • action (:keep, :reject)

    what to do with row matching criteria

  • field (Symbol)

    to match value in

  • value (String)

    value to match



57
58
59
60
61
62
# File 'lib/kiba/extend/transforms/filter_rows/field_equal_to.rb', line 57

def initialize(action:, field:, value:)
  validate_action_argument(action)
  @column = field
  @value = value
  @action = action
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


65
66
67
68
69
70
71
72
# File 'lib/kiba/extend/transforms/filter_rows/field_equal_to.rb', line 65

def process(row)
  case action
  when :keep
    (row.fetch(column, nil) == value) ? row : nil
  when :reject
    (row.fetch(column, nil) == value) ? nil : row
  end
end