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-3.0.3 lib/mustermann/regexp_based.rb
mustermann-3.0.2 lib/mustermann/regexp_based.rb
mustermann-3.0.1 lib/mustermann/regexp_based.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/regexp_based.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/regexp_based.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/regexp_based.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/regexp_based.rb
mustermann-3.0.0 lib/mustermann/regexp_based.rb
mustermann-2.0.2 lib/mustermann/regexp_based.rb
mustermann-2.0.1 lib/mustermann/regexp_based.rb
mustermann-2.0.0 lib/mustermann/regexp_based.rb
mustermann-1.1.2 lib/mustermann/regexp_based.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/mustermann-1.1.1/lib/mustermann/regexp_based.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/mustermann-1.1.1/lib/mustermann/regexp_based.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/regexp_based.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.0.3/lib/mustermann/regexp_based.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/regexp_based.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/mustermann-1.0.3/lib/mustermann/regexp_based.rb
mustermann-1.1.1 lib/mustermann/regexp_based.rb
mustermann-1.1.0 lib/mustermann/regexp_based.rb