Class: CSVDecision::Matchers::Pattern Private

Inherits:
Matcher
  • Object
show all
Defined in:
lib/csv_decision/matchers/pattern.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Match cell against a regular expression pattern - e.g., =~ hot|col or .OPT.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Matcher

#ins?, #outs?

Constructor Details

#initialize(options = {}) ⇒ Pattern

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Pattern.

Parameters:

  • options (Hash{Symbol=>Object}) (defaults to: {})

    Used to determine the value of regexp_implicit:.



78
79
80
81
# File 'lib/csv_decision/matchers/pattern.rb', line 78

def initialize(options = {})
  # By default regexp's must have an explicit comparator.
  @regexp_explicit = !options[:regexp_implicit]
end

Class Method Details

.matches?(cell, regexp_explicit:) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

(see Pattern#matches)

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/csv_decision/matchers/pattern.rb', line 65

def self.matches?(cell, regexp_explicit:)
  comparator, value = regexp?(cell: cell, explicit: regexp_explicit)

  # We could not find a regexp pattern - maybe it's a simple string or something else?
  return false unless comparator

  # No need for a regular expression if we have simple string inequality
  pattern = comparator == '!=' ? value : Matchers.regexp(value)

  Proc.new(type: :proc, function: PATTERN_LAMBDAS[comparator].curry[pattern].freeze)
end

Instance Method Details

#matches?(cell) ⇒ false, CSVDecision::Proc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Recognise a regular expression pattern - e.g., =~ on|off or !~ OPT.*. If the option regexp_implicit: true has been set, then cells may omit the =~ comparator so long as they contain non-word characters typically used in regular expressions such as * and ..

Parameters:

  • cell (String)

    Data row cell.

Returns:

  • (false, CSVDecision::Proc)

    Returns false if this cell is not a match; otherwise returns the CSVDecision::Proc object indicating if this is a constant or some type of function.



89
90
91
# File 'lib/csv_decision/matchers/pattern.rb', line 89

def matches?(cell)
  Pattern.matches?(cell, regexp_explicit: @regexp_explicit)
end