Rails Syntax for Mustermann

This gem implements the rails pattern type for Mustermann. It is compatible with Ruby on Rails, Journey, the http_router gem, Lotus and Scalatra (if configured)

Overview

Supported options: capture, except, greedy, space_matches_plus, uri_decode, version, and ignore_unknown_options.

External documentation: Ruby on Rails Guides: Routing.

require 'mustermann'

pattern = Mustermann.new('/:example', type: :rails)
pattern === "/foo.bar"     # => true
pattern === "/foo/bar"     # => false
pattern.params("/foo.bar") # => { "example" => "foo.bar" }
pattern.params("/foo/bar") # => nil

pattern = Mustermann.new('/:example(/:optional)', type: :rails)
pattern === "/foo.bar"     # => true
pattern === "/foo/bar"     # => true
pattern.params("/foo.bar") # => { "example" => "foo.bar", "optional" => nil   }
pattern.params("/foo/bar") # => { "example" => "foo",     "optional" => "bar" }

pattern = Mustermann.new('/*example', type: :rails)
pattern === "/foo.bar"     # => true
pattern === "/foo/bar"     # => true
pattern.params("/foo.bar") # => { "example" => "foo.bar" }
pattern.params("/foo/bar") # => { "example" => "foo/bar" }

Rails Compatibility

Rails syntax changed over time. You can target different Ruby on Rails versions by setting the version option to the desired Rails version.

The default is 4.2. Versions prior to 2.3 are not supported.

require 'mustermann'
Mustermann.new('/', type: :rails, version: "2.3")
Mustermann.new('/', type: :rails, version: "3.0.0")

require 'rails'
Mustermann.new('/', type: :rails, version: Rails::VERSION::STRING)

Syntax

Syntax Element Description
:name Captures anything but a forward slash in a semi-greedy fashion. Capture is named name. Capture behavior can be modified with tt>capture and greedy option.
*name Captures anything in a non-greedy fashion. Capture is named name.
(expression) Enclosed expression is optional. Not available in 2.3 compatibility mode.
/ Matches forward slash. Does not match URI encoded version of forward slash.
\x In 3.x compatibility mode and starting with 4.2: Matches x or URI encoded version of x. For instance \* matches *.
In 4.0 or 4.1 compatibility mode: \ is ignored, x is parsed normally.
| Starting with 3.2 compatibility mode, this will raise a `Mustermann::ParseError`. While Ruby on Rails happily parses this character, it will result in broken routes due to a buggy implementation.
any other character Matches exactly that character or a URI encoded version of it.