class Mustermann::RegexpBased

Superclass for patterns that internally compile to a regular expression. @see Mustermann::Pattern @abstract

Attributes

regexp[R]

@return [Regexp] regular expression equivalent to the pattern.

to_regexp[R]

@return [Regexp] regular expression equivalent to the pattern.

Public Class Methods

new(string, **options) click to toggle source

@param (see Mustermann::Pattern#initialize) @return (see Mustermann::Pattern#initialize) @see (see Mustermann::Pattern#initialize)

Calls superclass method Mustermann::Pattern::new
# 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

Public Instance Methods

peek_match(string) click to toggle source

@param (see Mustermann::Pattern#peek_match) @return (see Mustermann::Pattern#peek_match) @see (see Mustermann::Pattern#peek_match)

# File lib/mustermann/regexp_based.rb, line 35
def peek_match(string)
  @peek_regexp.match(string)
end
peek_size(string) click to toggle source

@param (see Mustermann::Pattern#peek_size) @return (see Mustermann::Pattern#peek_size) @see (see Mustermann::Pattern#peek_size)

# File lib/mustermann/regexp_based.rb, line 27
def peek_size(string)
  return unless match = peek_match(string)
  match.to_s.size
end

Private Instance Methods

compile(**options) click to toggle source
# File lib/mustermann/regexp_based.rb, line 42
def compile(**options)
  raise NotImplementedError, 'subclass responsibility'
end