Class: Kiba::Extend::Transforms::FilterRows::FieldMatchRegexp
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::FilterRows::FieldMatchRegexp
- Includes:
- ActionArgumentable
- Defined in:
- lib/kiba/extend/transforms/filter_rows/field_match_regexp.rb
Overview
Keep or reject rows where the value of the specified field matches the given regular expression. Matches across the entire field value. I.e. a multivalued field is not split into segments which are each tested for a match.
Examples
Source data:
{val: 'N'},
{val: 'n'},
{val: 'NN'},
{val: 'NY'},
{val: ''},
{val: nil}
Used in pipeline as:
transform FilterRows::FieldMatchRegexp, action: :keep, field: :val, match: '^N'
Resulting data:
{val: 'N'},
{val: 'NN'},
{val: 'NY'},
Used in pipeline as:
transform FilterRows::FieldMatchRegexp, action: :keep, field: :val, match: '^N', ignore_case: true
Resulting data:
{val: 'N'},
{val: 'n'},
{val: 'NN'},
{val: 'NY'},
Used in pipeline as:
transform FilterRows::FieldMatchRegexp, action: :reject, field: :val, match: '^N'
Resulting data:
{val: 'n'},
{val: ''},
{val: nil},
Instance Method Summary collapse
-
#initialize(action:, field:, match:, ignore_case: false) ⇒ FieldMatchRegexp
constructor
A new instance of FieldMatchRegexp.
-
#process(row) ⇒ Object
Constructor Details
#initialize(action:, field:, match:, ignore_case: false) ⇒ FieldMatchRegexp
Returns a new instance of FieldMatchRegexp.
75 76 77 78 79 80 81 |
# File 'lib/kiba/extend/transforms/filter_rows/field_match_regexp.rb', line 75 def initialize(action:, field:, match:, ignore_case: false) validate_action_argument(action) @action = action @field = field @match = ignore_case ? Regexp.new(match, Regexp::IGNORECASE) : Regexp.new(match) end |
Instance Method Details
#process(row) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kiba/extend/transforms/filter_rows/field_match_regexp.rb', line 84 def process(row) val = row.fetch(field) test = val ? val.match?(match) : false case action when :keep test ? row : nil when :reject test ? nil : row end end |