Created
February 20, 2012 13:22
-
-
Save jamiehodge/1869162 to your computer and use it in GitHub Desktop.
Mount Sinatra subclasses
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' | |
class Sinatra::Base | |
def self.mount(klass, route="/#{klass.name.downcase}s") | |
before "#{route}/*" do | |
halt klass.call( | |
env.merge!( | |
'SCRIPT_NAME' => route, | |
'PATH_INFO' => params.delete('splat').join('/'), | |
'rack.request.query_hash' => params | |
) | |
) | |
end | |
end | |
end | |
class Foo < Sinatra::Base | |
mount Foo, '/baz' # nest | |
%w{get post put delete patch options}.each do |method| | |
self.send method.to_sym, '/:foo_id' do | |
"#{method}: #{params}" | |
end | |
end | |
end | |
class Bar < Sinatra::Base | |
get '/' do | |
'index' | |
end | |
mount Foo, '/:bar_id/foos' | |
run! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment