Class: Kiba::Extend::Transforms::FilterRows::FieldPopulated

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

Overview

Keep or reject rows based on whether the given field is populated. Blank strings and nils count as not populated.

Examples

Source data:

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

Used in pipeline as:

transform FilterRows::FieldPopulated, action: :keep, field: :val

Resulting data:

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

Used in pipeline as:

transform FilterRows::FieldPopulated, action: :reject, field: :val

Resulting data:

{val:  ''},
{val:  nil},

Instance Method Summary collapse

Constructor Details

#initialize(action:, field:) ⇒ FieldPopulated

Returns a new instance of FieldPopulated.

Parameters:

  • action (:keep, :reject)

    what to do with row matching criteria

  • field (Symbol)

    to check populated status in



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

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

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


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

def process(row)
  val = row[field]

  case action
  when :keep
    val.blank? ? nil : row
  when :reject
    val.blank? ? row : nil
  end
end