Class: Merb::Router
- Defined in:
- lib/merb-core/dispatch/router.rb,
lib/merb-core/dispatch/router/route.rb,
lib/merb-core/dispatch/router/behavior.rb,
lib/merb-core/dispatch/router/resources.rb,
lib/merb-core/dispatch/router/cached_proc.rb
Overview
Router stores route definitions and finds the first route that matches the incoming request URL.
Then information from route is used by dispatcher to call action on the controller.
Routes compilation.
The most interesting method of Router (and heart of route matching machinery) is match method generated on the fly from routes definitions. It is called routes compilation. Generated match method body contains one if/elsif statement that picks the first matching route definition and sets values to named parameters of the route.
Compilation is synchronized by mutex.
Defined Under Namespace
Classes: Behavior, CachedProc, GenerationError, NotCompiledError, Route, RouteNotFound
Class Attribute Summary collapse
Class Method Summary collapse
-
.append(&block) ⇒ Object
Appends route in the block to routing table.
-
.match_before_compilation(request) ⇒ Object
(also: match)
Just a placeholder for the compiled match method.
-
.prepare(first = [], last = [], &block) ⇒ Object
Creates a route building context and evaluates the block in it.
-
.prepend(&block) ⇒ Object
Prepends routes in the block to routing table.
-
.reset! ⇒ Object
Clears the routing table.
-
.resource(*args) ⇒ Object
Generates a URL from the resource(s).
-
.route_for(request) ⇒ Object
Finds route matching URI of the request and returns a tuple of [route index, route params].
-
.url(name, *args) ⇒ Object
Generates a URL from the params.
Class Attribute Details
.named_routes ⇒ Object
39 40 41 |
# File 'lib/merb-core/dispatch/router.rb', line 39 def named_routes @named_routes end |
.resource_routes ⇒ Object
39 40 41 |
# File 'lib/merb-core/dispatch/router.rb', line 39 def resource_routes @resource_routes end |
.root_behavior ⇒ Object
39 40 41 |
# File 'lib/merb-core/dispatch/router.rb', line 39 def root_behavior @root_behavior end |
.routes ⇒ Object
39 40 41 |
# File 'lib/merb-core/dispatch/router.rb', line 39 def routes @routes end |
Class Method Details
.append(&block) ⇒ Object
Appends route in the block to routing table.
66 67 68 |
# File 'lib/merb-core/dispatch/router.rb', line 66 def append(&block) prepare(routes, [], &block) end |
.match_before_compilation(request) ⇒ Object Also known as: match
Just a placeholder for the compiled match method
110 111 112 |
# File 'lib/merb-core/dispatch/router.rb', line 110 def match_before_compilation(request) #:nodoc: raise NotCompiledError, "The routes have not been compiled yet" end |
.prepare(first = [], last = [], &block) ⇒ Object
Creates a route building context and evaluates the block in it. A copy of root_behavior (and instance of Behavior) is copied as the context.
Parameters
- first<Array>
-
An array containing routes that should be prepended to the routes defined in the block.
- last<Array>
-
An array containing routes that should be appended to the routes defined in the block.
Returns
- Merb::Router
-
Returns self to allow chaining of methods.
57 58 59 60 61 62 63 |
# File 'lib/merb-core/dispatch/router.rb', line 57 def prepare(first = [], last = [], &block) @routes = [] root_behavior.with_proxy(&block) @routes = first + @routes + last compile self end |
.prepend(&block) ⇒ Object
Prepends routes in the block to routing table.
71 72 73 |
# File 'lib/merb-core/dispatch/router.rb', line 71 def prepend(&block) prepare([], routes, &block) end |
.reset! ⇒ Object
Clears the routing table. Route generation and request matching won’t work anymore until a new routing table is built.
77 78 79 80 81 82 |
# File 'lib/merb-core/dispatch/router.rb', line 77 def reset! class << self alias_method :match, :match_before_compilation end self.routes, self.named_routes = [], {} end |
.resource(*args) ⇒ Object
Generates a URL from the resource(s)
Parameters
- resources<Symbol,Object>
-
The identifiers for the resource route to generate. These can either be symbols or objects. Symbols denote resource collection routes and objects denote the members.
- params<Hash>
-
Any extra parameters needed to generate the route.
Returns
- String
-
The generated URL
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/merb-core/dispatch/router.rb', line 168 def resource(*args) defaults = args.pop = (args) || {} key = [] params = [] args.each do |arg| if arg.is_a?(Symbol) || arg.is_a?(String) key << arg.to_s else key << arg.class.to_s params << arg end end params << unless route = Merb::Router.resource_routes[key] raise Merb::Router::GenerationError, "Resource route not found: #{args.inspect}" end route.generate(params, defaults) end |
.route_for(request) ⇒ Object
Finds route matching URI of the request and returns a tuple of [route index, route params]. This method is called by the dispatcher and isn’t as useful in applications.
Parameters
- request<Merb::Request>
-
request to match.
Returns
- <Array(Integer, Hash)
-
Two-tuple: route index and route parameters. Route parameters are :controller, :action and all the named segments of the route.
99 100 101 102 103 104 105 106 107 |
# File 'lib/merb-core/dispatch/router.rb', line 99 def route_for(request) #:nodoc: index, params = match(request) route = routes[index] if index if !route raise ControllerExceptions::NotFound, "No routes match the request: #{request.uri}" end [route, params] end |
.url(name, *args) ⇒ Object
Generates a URL from the params
Parameters
- name<Symbol>
-
The name of the route to generate
- anonymous_params<Object>
-
An array of anonymous parameters to generate the route with. These parameters are assigned to the route parameters in the order that they are passed.
- params<Hash>
-
Named parameters to generate the route with.
- defaults<Hash>
-
A hash of default parameters to generate the route with. This is usually the request parameters. If there are any required params that are missing to generate the route, they are pulled from this hash.
Returns
- String
-
The generated URL
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/merb-core/dispatch/router.rb', line 139 def url(name, *args) unless name.is_a?(Symbol) args.unshift(name) name = :default end unless route = Merb::Router.named_routes[name] raise Merb::Router::GenerationError, "Named route not found: #{name}" end defaults = args.pop route.generate(args, defaults) end |