class CSVDecision::Matchers::Pattern
Match cell against a regular expression pattern - e.g., +=~ hot|col+ or +.OPT.+
Constants
- EXPLICIT_COMPARISON
- IMPLICIT_COMPARISON
- PATTERN_LAMBDAS
Public Class Methods
matches?(cell, regexp_explicit:)
click to toggle source
@api private (see Pattern#matches)
# 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
new(options = {})
click to toggle source
@param options [Hash{Symbol=>Object}] Used to determine the value of regexp_implicit
:.
# 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
Private Class Methods
parse(comparator:, value:)
click to toggle source
# File lib/csv_decision/matchers/pattern.rb, line 40 def self.parse(comparator:, value:) return false if value.blank? # We cannot do a regexp comparison against a symbol name. return if value[0] == ':' # If no comparator then the implicit option must be on comparator = regexp_implicit(value) if comparator.nil? [comparator, value] end
regexp?(cell:, explicit:)
click to toggle source
# File lib/csv_decision/matchers/pattern.rb, line 26 def self.regexp?(cell:, explicit:) # By default a regexp pattern must use an explicit comparator match = explicit ? EXPLICIT_COMPARISON.match(cell) : IMPLICIT_COMPARISON.match(cell) return false if match.nil? comparator = match['comparator'] # Comparator may be omitted if the regexp_explicit option is off. return false if explicit && comparator.nil? parse(comparator: comparator, value: match['value']) end
regexp_implicit(value)
click to toggle source
# File lib/csv_decision/matchers/pattern.rb, line 53 def self.regexp_implicit(value) # rubocop: disable Style/CaseEquality return unless /\W/ === value # rubocop: enable Style/CaseEquality # Make the implicit comparator explicit '=~' end
Public Instance Methods
matches?(cell)
click to toggle source
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 .
. @param (see Matcher#matches?
) @return (see Matcher#matches?
)
# File lib/csv_decision/matchers/pattern.rb, line 89 def matches?(cell) Pattern.matches?(cell, regexp_explicit: @regexp_explicit) end