Class: Kiba::Extend::Transforms::FilterRows::AnyFieldsPopulated
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::FilterRows::AnyFieldsPopulated
- Includes:
- ActionArgumentable, Allable
- Defined in:
- lib/kiba/extend/transforms/filter_rows/any_fields_populated.rb
Overview
Keep or reject rows based on whether any of the given fields is 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::AnyFieldsPopulated, action: :keep, fields: %i[a b]
Resulting data:
{a: 'a', b: 'b', c: 'c' },
{a: 'a', b: 'b', c: '' },
{a: '', b: 'b', c: 'c' }
Used in pipeline as:
transform FilterRows::AnyFieldsPopulated, action: :keep, fields: :all
Resulting data:
{a: 'a', b: 'b', c: 'c' },
{a: 'a', b: 'b', c: '' },
{a: '', b: nil, c: 'c' },
{a: '', b: 'b', c: 'c' }
Used in pipeline as:
transform FilterRows::AnyFieldsPopulated, action: :reject, fields: %i[a b]
Resulting data:
{a: '', b: nil, c: 'c' },
{a: '', b: nil, c: nil }
Instance Method Summary collapse
-
#initialize(action:, fields:) ⇒ AnyFieldsPopulated
constructor
A new instance of AnyFieldsPopulated.
-
#process(row) ⇒ Object
Constructor Details
#initialize(action:, fields:) ⇒ AnyFieldsPopulated
Returns a new instance of AnyFieldsPopulated.
73 74 75 76 77 |
# File 'lib/kiba/extend/transforms/filter_rows/any_fields_populated.rb', line 73 def initialize(action:, fields:) validate_action_argument(action) @action = action @fields = [fields].flatten end |
Instance Method Details
#process(row) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kiba/extend/transforms/filter_rows/any_fields_populated.rb', line 80 def process(row) finalize_fields(row) unless fields_set case action when :keep row if any_populated?(row) when :reject row unless any_populated?(row) end end |