Class: Kiba::Extend::Utils::FieldValueMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/utils/field_value_matcher.rb

Overview

Callable service object returning true/false

If row does not contain the specified field at all, the result is always false.

A nil value for the specified field will match '' (matchmode: :plain) or '^$' (matchmode: :regexp)

matchmode: :plain (the default) tests for a full match. You can test for partial matches with matchmode: :regex. Wrapping a regex match with ^ and $ anchors will force a full match in regex match mode.

Examples

With params: field: :test, match: 'UNMAPPED'

{foo: 'bar'} => false, # field not present, always false
{test: nil} => false, # nil field value, always false
{test: ''} => false,
{test: 'UNMAPPED'} => true,
{test: 'UNMAPPED '} => true, # values are stripped
{test: '  UNMAPPED '} => true, # values are stripped
{test: 'Unmapped'} => false

With params: field: :test, match: 'UNMAPPED', strip: false

{test: 'UNMAPPED'} => true,
{test: 'UNMAPPED '} => false, # values are not stripped
{test: '  UNMAPPED '} => false, # values are not stripped
{test: 'Unmapped'} => false

With params: field: :test, match: 'UNMAPPED', casesensitive: false

{test: 'UNMAPPED'} => true,
{test: 'Unmapped'} => true

With params: field: :test, match: ''

{foo: 'bar'} => false,
{test: nil} => true,
{test: ''} => true,
{test: ' '} => true, # values are stripped
{test: '    '} => true, # values are stripped
{test: 'UNMAPPED'} => false

With params: field: :test, match: '', treat_as_null: '%NULL%'

{foo: 'bar'} => false,
{test: nil} => true,
{test: ''} => true,
{test: 'UNMAPPED'} => false,
{test: '%NULL%'} => true, # gets converted to empty value prior to matching
{test: ' %NULL% '} => true # gets converted to empty value prior to matching

With params: field: :test, match: '^$', treat_as_null: '%NULL%', matchmode: :regexp

{foo: 'bar'} => false,
{test: nil} => true,
{test: ''} => true,
{test: 'UNMAPPED'} => false,
{test: '%NULL%'} => true

With params: field: :test, match: 'Foo', delim: '|'

{foo: 'Foo'} => false,
{test: nil} => false,
{test: ''} => false,
{test: 'Foo'} => true,
{test: 'foo'} => false,
{test: 'Foo|bar'} => true,
{test: 'baz|Foo'} => true,
{test: ' Foo|bar'} => true,
{test: 'baz| Foo '} => true,
{test: '|Foo'} => true,
{test: 'Foo|'} => true,
{test: 'foo|'} => false,
{test: 'bar|baz'} => false

With params: field: :test, match: '^$', matchmode: :regex, delim: '|'

{test: 'foo|'} => true,
{test: 'foo||foo'} => true,
{test: 'foo| |foo'} => true,
{test: '|foo'} => true,
{test: 'foo|%NULL%'} => false,
{test: 'foo|%NULL%|foo'} => false,
{test: 'foo| %NULL%|foo'} => false,
{test: '%NULL%|foo'} => false,

With params: field: :test, match: '', delim: '|', treat_as_null: '%NULL%'

{test: 'foo|%NULL%|bar'} => true,
{test: 'foo||bar'} => true,
{test: 'foo| %NULL% |bar'} => true,
{test: 'foo|  |bar'} => true

With params: field: :test, match: '', delim: '|', treat_as_null: '%NULL%', strip: false

{test: 'foo|%NULL%|bar'} => true,
{test: 'foo||bar'} => true,
{test: 'foo| %NULL% |bar'} => false,
{test: 'foo|  |bar'} => false

With params: field: :test, match: '^fo+$', matchmode: :regex

{test: 'food'} => false,
{test: 'foo'} => true,
{test: ' foo '} => true, # becasue stripped
{test: 'Food'} => false,
{test: 'Foo'} => false,

With params: field: :test, match: '^fo+$', matchmode: :regex, delim: '|'

{test: 'foo'} => true,
{test: 'foo|bar'} => true,
{test: 'Foo|bar'} => false,
{test: 'drink|food'} => false

With params: field: :test, match: '^fo+', matchmode: :regex, delim: '|', casesensitive: false

{test: 'foo'} => true,
{test: 'foo|bar'} => true,
{test: 'Foo|bar'} => true,
{test: 'drink|food'} => true

With params: field: :test, match: 'Foo', delim: '|', multimode: :all

{foo: 'Foo'} => false,
{test: nil} => false,
{test: ''} => false,
{test: 'Foo'} => true,
{test: 'foo'} => false,
{test: 'Foo|Foo'} => true,
{test: 'Foo|bar'} => false,
{test: 'baz|Foo'} => false,
{test: ' Foo|bar'} => false,
{test: 'baz| Foo '} => false,
{test: '|Foo'} => true,
{test: 'Foo|'} => true,
{test: 'foo|'} => false,
{test: 'bar|baz'} => false

With params: field: :test, match: 'Foo', delim: '|', multimode: :allstrict

{foo: 'Foo'} => false,
{test: nil} => false,
{test: ''} => false,
{test: 'Foo'} => true,
{test: 'foo'} => false,
{test: 'Foo|Foo'} => true,
{test: 'Foo|bar'} => false,
{test: 'baz|Foo'} => false,
{test: ' Foo|bar'} => false,
{test: 'baz| Foo '} => false,
{test: '|Foo'} => false,
{test: 'Foo|'} => false,
{test: 'foo|'} => false,
{test: 'bar|baz'} => false

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FieldValueMatcher.

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 individual values prior to matching

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

    how a multivalue match is determined. If :any, result is true if any value matches. If :all, empty values are ignored and will return true if all populated values match. If :allstrict, empty values are not ignored and will return false if match value does not match them (since 3.0.0)



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/kiba/extend/utils/field_value_matcher.rb', line 209

def initialize(field:, match:, matchmode: :plain, delim: nil, treat_as_null: nil, casesensitive: true,
  strip: true, multimode: :any)
  @field = field
  @delim = delim
  @casesensitive = casesensitive
  @matchmode = matchmode
  @nullval = treat_as_null
  @strip = strip
  @match = (matchmode == :regexp) ? create_regexp_match(match) : create_plain_match(match)
  @multimode = multimode
end

Instance Method Details

#call(row) ⇒ Object

Parameters:

  • row (Hash{Symbol => String})


222
223
224
225
226
227
# File 'lib/kiba/extend/utils/field_value_matcher.rb', line 222

def call(row)
  return false unless row.key?(field)

  value = row[field]
  is_match?(prepare_value(value))
end