Class: Kiba::Extend::Transforms::Warn::UnlessFieldValueMatches

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

Overview

Prints single warning to STDOUT if the value of the given field does not match match the given value in any rows

Useful for getting notification that assumptions made during initial transformation development have changed in subsequent data.

Uses Utils::FieldValueMatcher to determine whether value matches. See that class’ documentation for examples/more details on parameters.

This transform warns if Utils::FieldValueMatcher does not find a match

Since:

  • 3.0.0

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: :all) ⇒ UnlessFieldValueMatches

Returns a new instance of UnlessFieldValueMatches.

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: :all)

Since:

  • 3.0.0



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kiba/extend/transforms/warn/unless_field_value_matches.rb', line 33

def initialize(field:, match:, matchmode: :plain, delim: nil, treat_as_null: nil, casesensitive: true,
  strip: true, multimode: :all)
  @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:

  • 3.0.0



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kiba/extend/transforms/warn/unless_field_value_matches.rb', line 46

def process(row)
  return row unless single_warnings.empty?
  return row if row[field].blank?

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

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