Class: Mustermann::RegexpBased Abstract

Inherits:
Pattern
  • Object
show all
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.

See Also:

Direct Known Subclasses

AST::Pattern, Regular

Constant Summary

Constants included from Mustermann

CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError

Instance Attribute Summary collapse

Attributes inherited from Pattern

#uri_decode

Instance Method Summary collapse

Methods inherited from Pattern

#+, #==, #===, #=~, #eql?, #expand, #hash, #match, #named_captures, #names, new, #params, #peek, #peek_params, supported?, supported_options, #to_proc, #to_s, #to_templates, #|

Methods included from Mustermann

[], new

Constructor Details

#initialize(string, **options) ⇒ Pattern

Returns a new instance of Pattern.

Parameters:

  • string (String)

    the string representation of the pattern

  • options (Hash)

    options for fine-tuning the pattern behavior

See Also:



17
18
19
20
21
22
# File 'lib/mustermann/regexp_based.rb', line 17

def initialize(string, **options)
  super
  regexp       = compile(**options)
  @peek_regexp = /\A#{regexp}/
  @regexp      = /\A#{regexp}\Z/
end

Instance Attribute Details

#regexpRegexp (readonly) Also known as: to_regexp

Returns regular expression equivalent to the pattern.

Returns:

  • (Regexp)

    regular expression equivalent to the pattern.



11
12
13
# File 'lib/mustermann/regexp_based.rb', line 11

def regexp
  @regexp
end

Instance Method Details

#peek_match(string) ⇒ MatchData, ...

Returns MatchData or similar object if the pattern matches.

Parameters:

  • string (String)

    The string to match against

Returns:

See Also:



35
36
37
# File 'lib/mustermann/regexp_based.rb', line 35

def peek_match(string)
  @peek_regexp.match(string)
end

#peek_size(string) ⇒ Integer?

Returns the number of characters that match.

Parameters:

  • string (String)

    The string to match against

Returns:

  • (Integer, nil)

    the number of characters that match



27
28
29
30
# File 'lib/mustermann/regexp_based.rb', line 27

def peek_size(string)
  return unless match = peek_match(string)
  match.to_s.size
end