Class: Mustermann::Hybrid

Inherits:
Sinatra show all
Defined in:
lib/mustermann/hybrid.rb

Overview

Hybrid pattern type that bridges Sinatra and Rails pattern syntax.

It supports all syntax elements of Sinatra, plus URI template-style placeholders, and changes the semantics of parenthesized groups to match Rails:

  • A group without a pipe operator is **implicitly optional**, even without a trailing ‘?`. So `/foo(/bar)` matches both `/foo/bar` and `/foo`.

  • A group with a pipe operator is not implicitly optional, to avoid the ambiguity of ‘/scope/(a|b)` also matching `/scope/`. Add a trailing `?` to make such a group optional explicitly: `/scope/(a|b)?`.

Examples:

Implicit optional group (no pipe)

require 'mustermann'
pattern = Mustermann.new('/foo(/bar)', type: :hybrid)
pattern === '/foo'     # => true
pattern === '/foo/bar' # => true

Non-optional group with pipe

pattern = Mustermann.new('/scope/(a|b)', type: :hybrid)
pattern === '/scope/a' # => true
pattern === '/scope/'  # => false

Explicitly optional group with pipe

pattern = Mustermann.new('/scope/(a|b)?', type: :hybrid)
pattern === '/scope/'  # => true

Nested implicit optional groups (Rails-style resource routing)

pattern = Mustermann.new('/:controller(/:action(/:id))', type: :hybrid)
pattern.params('/posts')        # => { "controller" => "posts" }
pattern.params('/posts/show')   # => { "controller" => "posts", "action" => "show" }
pattern.params('/posts/show/1') # => { "controller" => "posts", "action" => "show", "id" => "1" }

See Also:

Constant Summary

Constants included from Mustermann

CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError, TrieError

Instance Attribute Summary

Attributes inherited from RegexpBased

#regexp

Attributes inherited from Pattern

#uri_decode

Method Summary

Methods inherited from Sinatra

escape, #safe_string, #|

Methods inherited from AST::Pattern

#expand, #to_templates

Methods inherited from RegexpBased

#initialize, #match, #peek_match, #peek_size

Methods inherited from Pattern

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

Methods included from Mustermann

[], new

Constructor Details

This class inherits a constructor from Mustermann::RegexpBased