Created
June 20, 2012 11:34
-
-
Save jamiehodge/2959463 to your computer and use it in GitHub Desktop.
sinatra/mount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra/base' | |
module Sinatra | |
module Mount | |
def mount(app, route="/#{app.name.downcase}") | |
before "#{route}*" do | |
halt app.call( | |
env.merge( | |
'SCRIPT_NAME' => route.split('/').last, | |
'PATH_INFO' => params.delete('splat').join('/') | |
) | |
) | |
end | |
end | |
end | |
register Mount | |
end | |
class Bar < Sinatra::Base | |
get '/' do | |
'bar' | |
end | |
get %r{^/(?<bar>[^/]+)$} do | |
"bar: #{params[:bar]}" | |
end | |
end | |
class Foo < Sinatra::Base | |
register Sinatra::Mount | |
get '/' do | |
'foo' | |
end | |
p mount Bar, '/:foo/bars' | |
get %r{^/(?<foo>[^/]+)$} do | |
"foo: #{params[:foo]}" | |
end | |
end | |
Foo.run! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also usable implementation of mount.