Module: Mustermann::AST::FastPattern

Included in:
Rails, Sinatra
Defined in:
lib/mustermann/ast/fast_pattern.rb

Overview

Mixin for AST::Pattern subclasses that accelerates compilation and AST construction for “simple” patterns: only static path segments and unconstrained full-segment captures (e.g. /foo/:bar/baz/:id). Patterns with optional groups, constraints, or non-default options fall through to the full AST pipeline.

Instance Method Summary collapse

Instance Method Details

#match(string) ⇒ Object

Bypasses the generic build_match overhead for simple patterns: uses MatchData#named_captures directly and avoids match.to_s / post_match / pre_match calls (all no-ops for A…Z anchored regexps).



32
33
34
35
36
37
38
# File 'lib/mustermann/ast/fast_pattern.rb', line 32

def match(string)
  return super unless @fast_match
  return unless match = @regexp.match(string)
  params = match.named_captures
  params.transform_values! { |v| unescape(v) } if string.include?('%')
  Match.new(self, string, params)
end

#to_astObject

Public override: fast path for simple patterns, falls through to super otherwise. Must remain public to match AST::Pattern#to_ast visibility.



42
43
44
45
46
47
# File 'lib/mustermann/ast/fast_pattern.rb', line 42

def to_ast
  return super unless simple_pattern?
  ast = self.class.ast_cache.fetch(@string) { build_fast_ast }
  @param_converters ||= {}
  ast
end