Class: Kiba::Extend::Transforms::Warn::IfFieldValueMatches

Inherits:
Object
  • Object
show all
Includes:
SingleWarnable
Defined in:
lib/kiba/extend/transforms/warn/if_field_value_matches.rb

Overview

Prints single warning to STDOUT if the value of the given field matches the given value in any rows

Useful if you have skipped mapping/setting up transforms for certain values in an initial/staging data set, but need to ensure you will notice if later/production data includes new values that need attention.

Publicly available example of use in kiba-tms

Uses Utils::FieldValueMatcher to determine whether value matches. See that class’ documentation for examples

This transform warns if Utils::FieldValueMatcher finds a match.

Since:

  • 2.9.0.94

Instance Method Summary collapse

Methods included from SingleWarnable

single_warnings

Constructor Details

#initialize(field:, match:, matchmode: :plain, delim: nil, treat_as_null: nil, casesensitive: true, strip: true, multimode: :any) ⇒ IfFieldValueMatches

Returns a new instance of IfFieldValueMatches.

Parameters:

  • field (Symbol)

    whose value to match

  • match (String)

    expresses the match criteria

  • matchmode (:plain, :regex) (defaults to: :plain)

    If :regex, string is converted to a regular expression

  • delim (nil, String) (defaults to: nil)

    if a String is given, triggers multivalue matching, where field value is split and the match is run against each resulting value

  • treat_as_null (nil, String) (defaults to: nil)

    if given, the string will be converted to empty string for matching

  • casesensitive (Boolean) (defaults to: true)

    whether match cares about case

  • strip (Boolean) (defaults to: true)

    whether to strip leading/trailing spaces from values for matching

  • multimode (:any, :all, :allstrict) (defaults to: :any)

Since:

  • 2.9.0.94



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kiba/extend/transforms/warn/if_field_value_matches.rb', line 36

def initialize(field:, match:, matchmode: :plain, delim: nil, treat_as_null: nil, casesensitive: true,
  strip: true, multimode: :any)
  @field = field
  @match = match
  @matcher = Utils::FieldValueMatcher.new(
    field: field, match: match, matchmode: matchmode, delim: delim,
    treat_as_null: treat_as_null, casesensitive: casesensitive, strip: strip,
    multimode: multimode
  )
  setup_single_warning
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 2.9.0.94



49
50
51
52
53
54
55
56
57
58
# File 'lib/kiba/extend/transforms/warn/if_field_value_matches.rb', line 49

def process(row)
  return row unless single_warnings.empty?

  result = matcher.call(row)
  return row unless result

  msg = "One or more rows has #{field} value matching #{match}"
  add_single_warning(msg)
  row
end