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:

Match as string, case sensitive

# Used in pipeline as:
# transform Delete::FieldValueMatchingRegexp,
#   fields: %i[a b z],
#   match: "xx+"
xform = Delete::FieldValueMatchingRegexp.new(
  fields: %i[a b z], match: "xx+"
)
input = [
  {a: "xxxx a thing", b: "foo"},
  {a: "thing xxxx 123", b: "bar"},
  {a: "x thing", b: "xxxx"},
  {a: "y thing", b: "xXxX"},
  {a: "xxxxxxx thing", b: "baz"},
  {a: "", b: nil}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {a: nil, b: "foo"},
  {a: nil, b: "bar"},
  {a: "x thing", b: nil},
  {a: "y thing", b: "xXxX"},
  {a: nil, b: "baz"},
  {a: "", b: nil}
]
expect(result).to eq(expected)

Match as anchored string, case insensitive

# Used in pipeline as:
# transform Delete::FieldValueMatchingRegexp,
#   fields: %i[a b],
#   match: "^xx+", casesensitive: false
xform = Delete::FieldValueMatchingRegexp.new(
  fields: %i[a b], match: "^xx+", casesensitive: false
)
input = [
  {a: "an xxxx", b: "xXxXxXy"}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {a: "an xxxx", b: nil}
]
expect(result).to eq(expected)

Match as regexp, case sensitive

# Used in pipeline as:
# transform Delete::FieldValueMatchingRegexp,
#   fields: %i[a b z],
#   match: /xx+/
xform = Delete::FieldValueMatchingRegexp.new(
  fields: %i[a b z], match: /xx+/
)
input = [
  {a: "xxxx a thing", b: "foo"},
  {a: "thing xxxx 123", b: "bar"},
  {a: "x thing", b: "xxxx"},
  {a: "y thing", b: "xXxX"},
  {a: "xxxxxxx thing", b: "baz"},
  {a: "", b: nil}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {a: nil, b: "foo"},
  {a: nil, b: "bar"},
  {a: "x thing", b: nil},
  {a: "y thing", b: "xXxX"},
  {a: nil, b: "baz"},
  {a: "", b: nil}
]
expect(result).to eq(expected)

Match as anchored regex, case insensitive

# Used in pipeline as:
# transform Delete::FieldValueMatchingRegexp,
#   fields: %i[a b],
#   match: /^xx+/i
xform = Delete::FieldValueMatchingRegexp.new(
  fields: %i[a b], match: /^xx+/i, casesensitive: false
)
input = [
  {a: "an xxxx", b: "xXxXxXy"}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {a: "an xxxx", b: nil}
]
expect(result).to eq(expected)

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, Regexp)

    value to match. A String is converted to a regular expression pattern via Regexp.new(match)

  • casesensitive (Boolean) (defaults to: true)

    match mode



106
107
108
109
110
# File 'lib/kiba/extend/transforms/delete/field_value_matching_regexp.rb', line 106

def initialize(fields:, match:, casesensitive: true)
  @fields = [fields].flatten
  @casesensitive = casesensitive
  @match = set_match(match)
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


113
114
115
116
117
118
119
120
121
122
# File 'lib/kiba/extend/transforms/delete/field_value_matching_regexp.rb', line 113

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

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

  row
end