Class: Kiba::Extend::Transforms::Delete::FieldValueContainingString

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

Overview

Deletes full field value of all given fields that contain the given string. You can control whether match is case sensitive or not.

To be clear, contain = a partial match. Use FieldValueMatchingRegexp with anchors to trigger deletion via a full match.

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::FieldValueContainingString, fields: %i[a b], match: 'xxxx'

Results in:

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

Input table:

| a       | b       |
|---------+---------|
| y thing | xXxXxXy |

Used in pipeline as:

transform Delete::FieldValueContainingString, fields: :b, match: 'xxxx', casesensitive: false

Results in:

| a       | b   |
|---------+-----|
| y thing | nil |

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FieldValueContainingString.

Parameters:

  • fields (Array<Symbol>, Symbol)

    field(s) to delete from

  • match (String)

    value to match

  • casesensitive (Boolean) (defaults to: true)

    match mode



75
76
77
78
79
# File 'lib/kiba/extend/transforms/delete/field_value_containing_string.rb', line 75

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

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kiba/extend/transforms/delete/field_value_containing_string.rb', line 82

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

    prepped = casesensitive ? exval : exval.downcase
    row[field] = nil if prepped[match]
  end

  row
end