From 7ec40394c67beca01d2c41ca09905b209b87b28c Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Mon, 21 Feb 2011 11:21:59 +0100 Subject: [PATCH] document custom route matchers --- README.rdoc | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.rdoc b/README.rdoc index 26899c6bf5..e2dc1e9da4 100644 --- a/README.rdoc +++ b/README.rdoc @@ -151,6 +151,47 @@ That way we can for instance easily implement a streaming example: get('/') { Stream.new } +=== Custom Route Matchers + +As shown above, Sinatra ships with built-in support for using String patterns +and regular expressions as route matches. However, it does not stop there. You +can easily define your own matchers: + + class AllButPattern + Match = Struct.new(:captures) + + def initialize(except) + @except = except + @caputres = Match.new([]) + end + + def match(str) + @caputres unless @except === str + end + end + + def all_but(pattern) + AllButPattern.new(pattern) + end + + get all_but("/index") do + # ... + end + +Note that the above example might be over-engineered, as it can also be +expressed as: + + get // do + pass if request.path_info == "/index" + # ... + end + +Or, using negative look ahead: + + get %r{^(?!/index$)} do + # ... + end + == Static Files Static files are served from the ./public directory. You can specify