Sha256: 72947fcb8978a06f3fcd7923e5a4c9f694820c0ba829288aa374db5f359d478d

Contents?: true

Size: 1.36 KB

Versions: 27

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true
require 'mustermann/pattern'
require 'forwardable'

module Mustermann
  # Superclass for patterns that internally compile to a regular expression.
  # @see Mustermann::Pattern
  # @abstract
  class RegexpBased < Pattern
    # @return [Regexp] regular expression equivalent to the pattern.
    attr_reader :regexp
    alias_method :to_regexp, :regexp

    # @param (see Mustermann::Pattern#initialize)
    # @return (see Mustermann::Pattern#initialize)
    # @see (see Mustermann::Pattern#initialize)
    def initialize(string, **options)
      super
      regexp       = compile(**options)
      @peek_regexp = /\A#{regexp}/
      @regexp      = /\A#{regexp}\Z/
    end

    # @param (see Mustermann::Pattern#peek_size)
    # @return (see Mustermann::Pattern#peek_size)
    # @see (see Mustermann::Pattern#peek_size)
    def peek_size(string)
      return unless match = peek_match(string)
      match.to_s.size
    end

    # @param (see Mustermann::Pattern#peek_match)
    # @return (see Mustermann::Pattern#peek_match)
    # @see (see Mustermann::Pattern#peek_match)
    def peek_match(string)
      @peek_regexp.match(string)
    end

    extend Forwardable
    def_delegators :regexp, :===, :=~, :match, :names, :named_captures

    def compile(**options)
      raise NotImplementedError, 'subclass responsibility'
    end

    private :compile
  end
end

Version data entries

27 entries across 24 versions & 5 rubygems

Version Path
mustermann-1.0.3 lib/mustermann/regexp_based.rb
mustermann-1.0.2 lib/mustermann/regexp_based.rb
mustermann-1.0.2.rc2 lib/mustermann/regexp_based.rb
mustermann-1.0.2.rc1 mustermann/lib/mustermann/regexp_based.rb
mustermann-1.0.1 lib/mustermann/regexp_based.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/mustermann-1.0.0/lib/mustermann/regexp_based.rb
mustermann-1.0.0 lib/mustermann/regexp_based.rb