Module: Nitro::Router
- Included in:
- Dispatcher
- Defined in:
- lib/nitro/router.rb
Overview
Router mixin. Typically used to generate ‘nice’ urls. Nice urls apart from looking more beautiful are considered (?) more Search Engine friendly.
However, due to the power of Nitro’s intelligent dispatching mechanism, routing is almost never used! It is only needed for really special urls.
Example
can also be initialized by a YAML file through Configuration.
Router.rules = [
{:match => /rewritten\/url\/(.*)/, :controller => IdController, :action => :register, :param => :name},
{:match => %r{another/zelo/(.*)/(.*)}, :controller => AdminController, :action => :kick, :params => [:name, :age]}
]
or
Router.add_rule :match => /rewritten/url/(.*)/, :controller => IdController, :action => :register, :param => :name Router.add_rule :match => %ranother/zelo/(another/zelo/(.*)/(another/zelo/(.*)/(.*), :controller => AdminController, :action => :kick, :params => [:name, :age]
this is the global table that initializes each Router instance.
or (specialized per Router instance)
r.add_rule(:match => %rrewritten/url/(rewritten/url/(.*), :controller => IdController, :action => :register, :param => :name) r.add_rule(:match => %ranother/zelo/(another/zelo/(.*)/(another/zelo/(.*)/(.*), :controller => AdminController, :action => :kick, :params => [:name, :age]) r.add_rule(:match => %rcool/(cool/(.*)_(cool/(.*)_(.*)cool/(.*)_(.*).html, :controller => AdminController, :action => :long, :params => [:name, :age])
– gmosx, TODO: use a hash only instead of [rule, options] ++
Instance Attribute Summary collapse
-
#rules ⇒ Object
The rules map ‘nice URLs’ to real URLs that can be handled by the Dispatcher.
Class Method Summary collapse
- .add_rule(rule) ⇒ Object (also: <<)
Instance Method Summary collapse
-
#<< ⇒ Object
Add a route to the routing table.
-
#add_rule(rule) ⇒ Object
Add a route to the routing table.
- #add_rules(new_rules) ⇒ Object
-
#decode_route(url) ⇒ Object
(also: #route)
Decodes a url to a [controller, action, params] tupple.
-
#encode_route(controller, action, *params) ⇒ Object
Encodes a [controller, action, params] tupple into a url.
-
#init_routes ⇒ Object
Initialize the routing table by importing from the global Router rules.
Instance Attribute Details
#rules ⇒ Object
The rules map ‘nice URLs’ to real URLs that can be handled by the Dispatcher.
58 59 60 |
# File 'lib/nitro/router.rb', line 58 def rules @rules end |
Class Method Details
.add_rule(rule) ⇒ Object Also known as: <<
123 124 125 |
# File 'lib/nitro/router.rb', line 123 def add_rule(rule) self.rules << rule end |
Instance Method Details
#<< ⇒ Object
Add a route to the routing table.
116 117 118 |
# File 'lib/nitro/router.rb', line 116 def add_rule(rule) @rules << rule end |
#add_rule(rule) ⇒ Object
Add a route to the routing table.
113 114 115 |
# File 'lib/nitro/router.rb', line 113 def add_rule(rule) @rules << rule end |
#add_rules(new_rules) ⇒ Object
118 119 120 |
# File 'lib/nitro/router.rb', line 118 def add_rules(new_rules) @rules.concat(new_rules) end |
#decode_route(url) ⇒ Object Also known as: route
Decodes a url to a [controller, action, params] tupple. Returns false if no decoding is possible.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/nitro/router.rb', line 70 def decode_route(url) # Front end server (for example Apache) some times escape # the url. url = CGI.unescape(url) for rule in @rules if md = url.match(rule[:match]) params = nil if param_names = rule[:params] || rule[:param] param_names = [ param_names ] unless param_names.is_a?(Array) params = Dictionary.new md.captures.each_with_index do |val, idx| params << [param_names[idx].to_s, val] end end Logger.debug "Rewriting '#{url}' to '#{rule[:controller]}##{rule[:action]}(#{params.values.join(', ')})." if $DBG return rule[:controller], rule[:action], params end end return false end |
#encode_route(controller, action, *params) ⇒ Object
Encodes a [controller, action, params] tupple into a url. Returns false if no encoding is possible.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/nitro/router.rb', line 96 def encode_route(controller, action, *params) if rule = @rules.find { |r| (r[:controller] == controller) and (r[:action] == action) } url = rule[:match].source (params.size / 2).times do |i| val = params[i + i + 1] url.sub!(/\(.*?\)/, val.to_s) end return url end return false end |