Class: Kiba::Extend::Transforms::FilterRows::FieldMatchRegexp

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(action:, field:, match:, ignore_case: false) ⇒ FieldMatchRegexp

Returns a new instance of FieldMatchRegexp.

Parameters:

  • action (:keep, :reject)

    what to do with row matching criteria

  • field (Symbol)

    to match value in

  • match (String)

    value that will be turned into a Regexp using Regexp.new(match)

  • ignore_case (Boolean) (defaults to: false)

    controls case sensitivity of matching



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

Parameters:

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


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