Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created February 20, 2012 13:22
Show Gist options
  • Save jamiehodge/1869162 to your computer and use it in GitHub Desktop.
Save jamiehodge/1869162 to your computer and use it in GitHub Desktop.
Mount Sinatra subclasses
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