lib/csv_decision/matchers/pattern.rb in csv_decision-0.0.6 vs lib/csv_decision/matchers/pattern.rb in csv_decision-0.0.7

- old
+ new

@@ -1,12 +1,14 @@ # frozen_string_literal: true # CSV Decision: CSV based Ruby decision tables. -# Created December 2017 by Brett Vickers +# Created December 2017. +# @author Brett Vickers <brett@phillips-vickers.com> # See LICENSE and README.md for details. module CSVDecision # Methods to assign a matcher to data cells + # @api private class Matchers # Match cell against a regular expression pattern - e.g., +=~ hot|col+ or +.*OPT.*+ class Pattern < Matcher EXPLICIT_COMPARISON = Matchers.regexp("(?<comparator>=~|!~|!=)\\s*(?<value>\\S.*)") private_constant :EXPLICIT_COMPARISON @@ -47,20 +49,24 @@ # If no comparator then the implicit option must be on comparator = regexp_implicit(value) if comparator.nil? [comparator, value] end + private_class_method :parse def self.regexp_implicit(value) # rubocop: disable Style/CaseEquality return unless /\W/ === value # rubocop: enable Style/CaseEquality - # Make the implict comparator explict + # Make the implicit comparator explicit '=~' end + private_class_method :regexp_implicit + # @api private + # (see Pattern#matches) 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 @@ -77,11 +83,12 @@ # By default regexp's must have an explicit comparator @regexp_explicit = !options[:regexp_implicit] end # 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 +.+. + # 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 Matchers::Matcher#matches?) # @return (see Matchers::Matcher#matches?) def matches?(cell) Pattern.matches?(cell, regexp_explicit: @regexp_explicit) end \ No newline at end of file