Module: Raw::RouterMixin

Included in:
Router
Defined in:
lib/raw/dispatcher/router.rb

Overview

Routing (path rewriting) functionality. Due to the power of Nitro’s intelligent dispatching mechanism, routing is almost never used! It is only needed for really special urls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rulesObject

The routing rules.



13
14
15
# File 'lib/raw/dispatcher/router.rb', line 13

def rules
  @rules
end

Instance Method Details

#add_rule(rule) ⇒ Object Also known as: <<, add_route

Add a routing rule

Examples

r.add_rule(:match => %rRaw::RouterMixin.^~(^~(.*), :controller => User::Controller, :action => view, :params => [:name])



21
22
23
24
# File 'lib/raw/dispatcher/router.rb', line 21

def add_rule(rule)
  @rules ||= []
  @rules << rule
end

#add_rules_from_annotations(controller) ⇒ Object

Convert routes defined with annotations to normal routes. Typically called after a new Controller is mounted.

Example of routing through annotations

def view_user

"params:  #{request[:id]} and #{request[:mode]}"

end ann :view_user, :match => /user_(d*)_(*?).html/, :params => [:id, :mode]



52
53
54
55
56
57
58
59
# File 'lib/raw/dispatcher/router.rb', line 52

def add_rules_from_annotations(controller)
  for m in controller.action_methods
    m = m.to_sym
    if match = controller.ann(m, :match)
      add_rule(:match => match, :controller => controller, :action => m, :params => controller.ann(m, :params))
    end
  end
end

#decode_route(path) ⇒ Object Also known as: route

Try to decode the given path by applying routing rules. If routing is possible return the transformed path, else return the input path.



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

def decode_route(path)
  # Front end server (for example Apache) some times escape 
  # the uri. REMOVE THIS: unescape in the adapter!!
  path = CGI.unescape(path)

  for rule in @rules
    unless transformer = rule[:transformer]
      transformer = rule[:transformer] = rule_transformer(rule)
    end
  
    if path.gsub!(rule[:match], transformer)
      Logger.debug "Rewriting '#{path}'." if $DBG
      break;
    end
  end
  
  return path
end

#encode_route(controller, action, *params) ⇒ Object

Encodes a [controller, action, params] tupple into a path. Returns false if no encoding is possible.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/raw/dispatcher/router.rb', line 88

def encode_route(controller, action, *params)
  if rule = @rules.find { |r| (r[:controller] == controller) and (r[:action] == action) }
    path = rule[:match].source

    (params.size / 2).times do |i|
      val = params[i + i + 1]
      path.sub!(/\(.*?\)/, val.to_s)
    end       
    
    return path
  end
  
  return false
end

#rule_transformer(rule) ⇒ Object

Generate the transformer string for the match gsub.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/raw/dispatcher/router.rb', line 30

def rule_transformer(rule)
  transformer = "#{rule[:controller].mount_path}/#{rule[:action]}"
  
  if params = rule[:params]
    params.size.times do |i|
      transformer << "/#{i+1}" 
    end
  end
      
  return transformer
end