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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rulesObject

The rules map ‘nice URLs’ to real URLs that can be handled by the Dispatcher.



52
53
54
# File 'lib/nitro/router.rb', line 52

def rules
  @rules
end

Class Method Details

.add_rule(rule) ⇒ Object Also known as: <<



113
114
115
# File 'lib/nitro/router.rb', line 113

def add_rule(rule)
  self.rules << rule
end

Instance Method Details

#<<Object

Add a route to the routing table.



106
107
108
# File 'lib/nitro/router.rb', line 106

def add_rule(rule)
  @rules << rule
end

#add_rule(rule) ⇒ Object

Add a route to the routing table.



103
104
105
# File 'lib/nitro/router.rb', line 103

def add_rule(rule)
  @rules << rule
end

#add_rules(new_rules) ⇒ Object



108
109
110
# File 'lib/nitro/router.rb', line 108

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nitro/router.rb', line 64

def decode_route(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 = {}
        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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nitro/router.rb', line 86

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

#init_routesObject

Initialize the routing table by importing from the global Router rules.



57
58
59
# File 'lib/nitro/router.rb', line 57

def init_routes
  @rules = Router.rules.dup
end