Parent

Journey::Formatter

 

The Formatter class is used for formatting URLs. For example, parameters passed to url_for in rails will eventually call Formatter#generate

Attributes

routes[R]

Public Class Methods

new(routes) click to toggle source
    # File lib/journey/formatter.rb, line 8
 8:     def initialize routes
 9:       @routes = routes
10:       @cache  = nil
11:     end

Public Instance Methods

clear() click to toggle source
    # File lib/journey/formatter.rb, line 49
49:     def clear
50:       @cache = nil
51:     end
generate(key, name, options, recall = {}) click to toggle source
    # File lib/journey/formatter.rb, line 13
13:     def generate key, name, options, recall = {}, parameterize = nil
14:       constraints = recall.merge options
15: 
16:       match_route(name, constraints) do |route|
17:         data = constraints.dup
18: 
19:         keys_to_keep = route.parts.reverse.drop_while { |part|
20:           !options.key?(part) || (options[part] || recall[part]).nil?
21:         } | route.required_parts
22: 
23:         (data.keys - keys_to_keep).each do |bad_key|
24:           data.delete bad_key
25:         end
26: 
27:         parameterized_parts = data.dup
28: 
29:         if parameterize
30:           parameterized_parts.each do |k,v|
31:             parameterized_parts[k] = parameterize.call(k, v)
32:           end
33:         end
34: 
35:         parameterized_parts.keep_if { |_,v| v  }
36: 
37:         next if !name && route.requirements.empty? && route.parts.empty?
38: 
39:         next unless verify_required_parts!(route, parameterized_parts)
40: 
41:         z = Hash[options.to_a - data.to_a - route.defaults.to_a]
42: 
43:         return [route.format(parameterized_parts), z]
44:       end
45: 
46:       raise Router::RoutingError
47:     end

Private Instance Methods

build_cache() click to toggle source
     # File lib/journey/formatter.rb, line 114
114:     def build_cache
115:       kash = {}
116:       routes.each_with_index do |route, i|
117:         money = kash
118:         route.required_defaults.each do |tuple|
119:           hash = (money[tuple] ||= {})
120:           money = hash
121:         end
122:         (money[:___routes] ||= []) << [i, route]
123:       end
124:       kash
125:     end
cache() click to toggle source
     # File lib/journey/formatter.rb, line 127
127:     def cache
128:       @cache ||= build_cache
129:     end
match_route(name, options) click to toggle source
    # File lib/journey/formatter.rb, line 58
58:     def match_route name, options
59:       if named_routes.key? name
60:         yield named_routes[name]
61:       else
62:         #routes = possibles(@cache, options.to_a)
63:         routes = non_recursive(cache, options.to_a)
64: 
65:         hash = routes.group_by { |_, r|
66:           r.score options
67:         }
68: 
69:         hash.keys.sort.reverse_each do |score|
70:           next if score < 0
71: 
72:           hash[score].sort_by { |i,_| i }.each do |_,route|
73:             yield route
74:           end
75:         end
76:       end
77:     end
named_routes() click to toggle source
    # File lib/journey/formatter.rb, line 54
54:     def named_routes
55:       routes.named_routes
56:     end
non_recursive(cache, options) click to toggle source
    # File lib/journey/formatter.rb, line 79
79:     def non_recursive cache, options
80:       routes = []
81:       stack  = [cache]
82: 
83:       while stack.any?
84:         c = stack.shift
85:         routes.concat c[:___routes] if c.key? :___routes
86: 
87:         options.each do |pair|
88:           stack << c[pair] if c.key? pair
89:         end
90:       end
91: 
92:       routes
93:     end
possibles(cache, options, depth = 0) click to toggle source
     # File lib/journey/formatter.rb, line 95
 95:     def possibles cache, options, depth = 0
 96:       cache.fetch(:___routes) { [] } + options.find_all { |pair|
 97:         cache.key? pair
 98:       }.map { |pair|
 99:         possibles(cache[pair], options, depth + 1)
100:       }.flatten(1)
101:     end
verify_required_parts!(route, parts) click to toggle source
     # File lib/journey/formatter.rb, line 103
103:     def verify_required_parts! route, parts
104:       tests = route.path.requirements
105:       route.required_parts.all? { |key|
106:         if tests.key? key
107:           /\A#{tests[key]}\Z/ === parts[key]
108:         else
109:           parts.fetch(key) { false }
110:         end
111:       }
112:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.