Created
May 13, 2011 17:54
-
-
Save cararemixed/970992 to your computer and use it in GitHub Desktop.
Better routes
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
uri 'contacts', name: 'contacts' do | |
get 'contacts#index' | |
end | |
uri 'contacts/:id/addresses', name: 'contact_addresses' do | |
put 'contact_addresses#replace' | |
post 'contact_addresses#create' | |
delete 'contact_addresses#delete' | |
end |
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
module URIMap | |
class URITemplate | |
def delete(handler) | |
@methods << [:delete, handler] | |
end | |
def draw(routes) | |
opts = @name ? {} : {as: @name} | |
@methods.each do |(method, handler)| | |
routes.send(method, opts.merge(@template => handler)) | |
end | |
end | |
def get(handler) | |
@methods << [:get, handler] | |
end | |
def post(handler) | |
@methods << [:post, handler] | |
end | |
def put(handler) | |
@methods << [:put, handler] | |
end | |
private | |
def initialize(template, opts) | |
@template = template | |
@name = opts[:name] | |
@methods = [] | |
end | |
end | |
def uri(pattern, opts = {}, &b) | |
template = URITemplate.new(pattern, opts = {}) | |
template.instance_eval(&b) | |
template.draw(self) | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment