Class: Mustermann::RegexpBased Abstract
- Extended by:
- Forwardable
- Defined in:
- lib/mustermann/regexp_based.rb
Overview
This class is abstract.
Superclass for patterns that internally compile to a regular expression.
Direct Known Subclasses
Constant Summary
Constants included from Mustermann
CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError, TrieError
Instance Attribute Summary collapse
-
#regexp ⇒ Regexp
(also: #to_regexp)
readonly
Regular expression equivalent to the pattern.
Attributes inherited from Pattern
Instance Method Summary collapse
-
#initialize(string, **options) ⇒ Pattern
constructor
A new instance of Pattern.
- #match(string) ⇒ Object
-
#peek_match(string) ⇒ Mustermann::Match?
MatchData or similar object if the pattern matches.
-
#peek_size(string) ⇒ Integer?
The number of characters that match.
Methods inherited from Pattern
#+, #==, #===, #=~, #eql?, #expand, #hash, #identity_params?, new, #params, #peek, #peek_params, supported?, supported_options, #to_proc, #to_s, #to_templates, #|
Methods included from Mustermann
Constructor Details
#initialize(string, **options) ⇒ Pattern
Returns a new instance of Pattern.
17 18 19 20 21 22 23 |
# File 'lib/mustermann/regexp_based.rb', line 17 def initialize(string, **) super regexp = compile(**) @peek_regexp = /\A#{regexp}/ @regexp = /\A#{regexp}\Z/ @simple_captures = @regexp.named_captures.none? { |name, positions| positions.size > 1 || always_array?(name) } end |
Instance Attribute Details
#regexp ⇒ Regexp (readonly) Also known as: to_regexp
Returns regular expression equivalent to the pattern.
11 12 13 |
# File 'lib/mustermann/regexp_based.rb', line 11 def regexp @regexp end |
Instance Method Details
#match(string) ⇒ Object
38 39 40 41 |
# File 'lib/mustermann/regexp_based.rb', line 38 def match(string) return unless match = @regexp.match(string) Match.new(self, string, build_params(match)) end |
#peek_match(string) ⇒ Mustermann::Match?
Returns MatchData or similar object if the pattern matches.
36 |
# File 'lib/mustermann/regexp_based.rb', line 36 def peek_match(string) = build_match(@peek_regexp.match(string)) |
#peek_size(string) ⇒ Integer?
Returns the number of characters that match.
28 29 30 31 |
# File 'lib/mustermann/regexp_based.rb', line 28 def peek_size(string) return unless match = peek_match(string) match.to_s.size end |