Class: Kiba::Extend::Transforms::Delete::FieldValueMatchingRegexp

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/delete/field_value_matching_regexp.rb

Overview

Deletes full field value of all given fields that match the given regular expression pattern. You can control whether the regexp is case sensitive or not

Examples

Input table:

| a              | b    |
|----------------+------|
| xxxx a thing   | foo  |
| thing xxxx 123 | bar  |
| x thing        | xxxx |
| y thing        | xXxX |
| xxxxxxx thing  | baz  |
|                | nil  |

Used in pipeline as:

transform Delete::FieldValueMatchingRegexp, fields: %i[a b], match: 'xx+'

Results in:

| a       | b    |
|---------+------|
| nil     | foo  |
| nil     | bar  |
| x thing | nil  |
| y thing | xXxX |
| nil     | baz  |
|         | nil  |

Input table:

| a       | b       |
|---------+---------|
| an xxxx | xXxXxXy |

Used in pipeline as:

transform Delete::FieldValueMatchingRegexp, fields: %i[a b], match: '^xx+', casesensitive: false

Results in:

| a       | b   |
|---------+-----|
| an xxxx | nil |

Instance Method Summary collapse

Constructor Details

#initialize(fields:, match:, casesensitive: true) ⇒ FieldValueMatchingRegexp

Returns a new instance of FieldValueMatchingRegexp.

Parameters:

  • fields (Array<Symbol>, Symbol)

    field(s) to delete from

  • match (String)

    value to match. Is converted to a regular expression pattern via Regexp.new(match)

  • casesensitive (Boolean) (defaults to: true)

    match mode



72
73
74
75
76
# File 'lib/kiba/extend/transforms/delete/field_value_matching_regexp.rb', line 72

def initialize(fields:, match:, casesensitive: true)
  @fields = [fields].flatten
  @match = casesensitive ? Regexp.new(match) : Regexp.new(match,
    Regexp::IGNORECASE)
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


79
80
81
82
83
84
85
86
87
88
# File 'lib/kiba/extend/transforms/delete/field_value_matching_regexp.rb', line 79

def process(row)
  fields.each do |field|
    val = row.fetch(field)
    next if val.blank?

    row[field] = nil if val.match?(match)
  end

  row
end