Class: CSVDecision::Matchers::Pattern
- Defined in:
- lib/csv_decision/matchers/pattern.rb
Overview
Match cell against a regular expression pattern - e.g., =~ hot|col or .OPT.
Class Method Summary collapse
- .matches?(cell, regexp_explicit:) ⇒ Boolean
- .parse(comparator:, value:) ⇒ Object
- .regexp_implicit(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Pattern
constructor
A new instance of Pattern.
-
#matches?(cell) ⇒ false, CSVDecision::Proc
Recognise a regular expression pattern - e.g., =~ on|off or !~ OPT.*.
Constructor Details
#initialize(options = {}) ⇒ Pattern
Returns a new instance of Pattern
76 77 78 79 |
# File 'lib/csv_decision/matchers/pattern.rb', line 76 def initialize( = {}) # By default regexp's must have an explicit comparator @regexp_explicit = ![:regexp_implicit] end |
Class Method Details
.matches?(cell, regexp_explicit:) ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/csv_decision/matchers/pattern.rb', line 62 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.with(type: :proc, function: PATTERN_LAMBDAS[comparator].curry[pattern].freeze) end |
.parse(comparator:, value:) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# 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. # (Maybe we should add this feature?) 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_implicit(value) ⇒ Object
53 54 55 56 57 58 59 60 |
# 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 implict comparator explict '=~' end |
Instance Method Details
#matches?(cell) ⇒ false, CSVDecision::Proc
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 .
.
86 87 88 |
# File 'lib/csv_decision/matchers/pattern.rb', line 86 def matches?(cell) Pattern.matches?(cell, regexp_explicit: @regexp_explicit) end |