lib/sinatra/named_routes.rb in sinatra-named-routes-0.1.0 vs lib/sinatra/named_routes.rb in sinatra-named-routes-0.1.1
- old
+ new
@@ -3,11 +3,13 @@
module Sinatra
module NamedRoutes
module Helpers
- # def uri(addr = nil, absolute = true, add_script_name = true, params = {})
+ # Generates the absolute URI for a given path in the app.
+ # Takes Rack routers and reverse proxies into account.
+ # Extended support passing in named route and parameters.
def uri(*args)
path = args.shift if args.first.is_a? Symbol
params = args.pop if args.last.is_a? Array or args.last.is_a? Hash
if path
@@ -17,17 +19,21 @@
else
super(*args)
end
end
+
alias :to :uri
alias :url :uri
end
+ # Maps a path to name.
def map(name, path)
NamedRoutes.routes[name] = Route.new path
end
+
+ alias :bind :map
private
def route(verb, path, options={}, &block)
if path.is_a?(Symbol)
@@ -36,9 +42,10 @@
super(verb, path, options, &block)
end
def self.get_path(name, params = {})
+ raise ArgumentError, "No route with the name #{name} exists." if NamedRoutes.routes.nil?
NamedRoutes.routes[name].build params
end
def self.routes
@@routes ||= {}
\ No newline at end of file