Class: Kiba::Extend::Transforms::FilterRows::AllFieldsPopulated

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

Overview

Keep or reject rows based on whether all of the given fields are populated. Blank strings and nils count as not populated.

Examples

Source data:

{a: 'a', b: 'b', c: 'c' },
{a: 'a', b: 'b', c: '' },
{a: '', b: nil, c: 'c' },
{a: '', b: 'b', c: 'c' },
{a: '', b: nil, c: nil },

Used in pipeline as:

transform FilterRows::AllFieldsPopulated, action: :keep, fields: %i[a b]

Resulting data:

{a: 'a', b: 'b', c: 'c' },
{a: 'a', b: 'b', c: '' },

Used in pipeline as:

transform FilterRows::AllFieldsPopulated, action: :keep, fields: :all

Resulting data:

{a: 'a', b: 'b', c: 'c' }

Used in pipeline as:

transform FilterRows::AllFieldsPopulated, action: :reject, fields: %i[a b]

Resulting data:

{a: '', b: nil, c: 'c' },
{a: '', b: 'b', c: 'c' },
{a: '', b: nil, c: nil }

Since:

  • 2.9.0

Instance Method Summary collapse

Constructor Details

#initialize(action:, fields:) ⇒ AllFieldsPopulated

Returns a new instance of AllFieldsPopulated.

Parameters:

  • action (:keep, :reject)

    what to do with row matching criteria

  • fields (Array<Symbol>, :all)

    to check populated status in

Since:

  • 2.9.0



70
71
72
73
74
# File 'lib/kiba/extend/transforms/filter_rows/all_fields_populated.rb', line 70

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

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 2.9.0



77
78
79
80
81
82
83
84
85
86
# File 'lib/kiba/extend/transforms/filter_rows/all_fields_populated.rb', line 77

def process(row)
  finalize_fields(row) unless fields_set

  case action
  when :keep
    row if all_populated?(row)
  when :reject
    row unless all_populated?(row)
  end
end