I’ve published Mustermann 4.0, exactly 13 years after the first release!
What is Mustermann?
Mustermann is your personal string matching expert. As an expert in the field of strings and patterns, Mustermann keeps its runtime dependencies to a minimum and is fully covered with specs and documentation.
Given a string pattern, Mustermann will turn it into an object that behaves like a regular expression and has comparable performance characteristics.
if '/foo/bar' =~ Mustermann.new('/foo/*')
puts 'it works!'
end
case 'something.png'
when Mustermann.new('foo/*') then puts "prefixed with foo"
when Mustermann.new('*.pdf') then puts "it's a PDF"
when Mustermann.new('*.png') then puts "it's an image"
end
pattern = Mustermann.new('/:prefix/*.*')
pattern.params('/a/b.c') # => { "prefix" => "a", splat => ["b", "c"] }
Mustermann is used by projects like Sinatra, Hanami, and Grape.
New features
New features include:
- Massively improved performance
Mustermann::Set– a new way to match against multiple patterns at once.- The return of
Mustermann::Router– a tiny Rack router, leveragingMustermann::Set - Captures can now be mapped directly to certain core classes or well-known types, like
Itegeror UUIDs Mustermann::Hybrid– a quasi-superset of Sinatra and Rails pattern syntax- Did I mention the performance?
See the changelog for a full list.
Example usage of new features
require "mustermann/set"
set = Mustermann::Set.new(type: :hybrid, capture: { id: Integer, user_id: Integer, slug: :slug })
# adding values is optional
set.add "/users", "users.index"
set.add "/users/:id", "users.show"
set.add "/posts", "posts.index"
set.add "/users/:user_id/posts", "posts.index"
set.add "/posts/:id(-:slug)", "posts.show" # slug is optional
match = set.match("/posts/42-awesome-post")
# id is automatically converted to an Integer, and slug is available as a string
match.params # => { id: 42, slug: "awesome-post" }
# You can access the pattern and value that matched
match.value # => "posts.show"
match.pattern # => #<Mustermann::Hybrid:"/posts/:id(-:slug)">
# Generate a path from a set value and params
set.expand("posts.index") # => "/posts"
set.expand("posts.index", user_id: 42) # => "/users/42/posts"
Performance numbers from the release announcement
You can find more details in my blog post.
