Module: Rack::Mount::Generation::RouteSet
- Included in:
- RouteSet
- Defined in:
- lib/rack/mount/generation/route_set.rb
Instance Method Summary collapse
-
#add_route(*args) ⇒ Object
Adds generation aspects to RouteSet#add_route.
-
#generate(method, *args) ⇒ Object
:nodoc:.
-
#initialize(*args) ⇒ Object
Adds generation related concerns to RouteSet.new.
-
#rehash ⇒ Object
:nodoc:.
-
#url(*args) ⇒ Object
Generates path from identifiers or significant keys.
Instance Method Details
#add_route(*args) ⇒ Object
Adds generation aspects to RouteSet#add_route.
16 17 18 19 20 21 |
# File 'lib/rack/mount/generation/route_set.rb', line 16 def add_route(*args) route = super @named_routes[route.name] = route if route.name @generation_key_analyzer << route.generation_keys route end |
#generate(method, *args) ⇒ Object
:nodoc:
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rack/mount/generation/route_set.rb', line 57 def generate(method, *args) #:nodoc: raise 'route set not finalized' unless @generation_graph named_route, params, recall, = extract_params!(*args) merged = recall.merge(params) route = nil if named_route if route = @named_routes[named_route.to_sym] recall = route.defaults.merge(recall) url = route.generate(method, params, recall, ) [url, params] else raise RoutingError, "#{named_route} failed to generate from #{params.inspect}" end else keys = @generation_keys.map { |key| if k = merged[key] k.to_s else nil end } @generation_graph[*keys].each do |r| next unless r.significant_params? if url = r.generate(method, params, recall, ) return [url, params] end end raise RoutingError, "No route matches #{params.inspect}" end end |
#initialize(*args) ⇒ Object
Adds generation related concerns to RouteSet.new.
8 9 10 11 12 13 |
# File 'lib/rack/mount/generation/route_set.rb', line 8 def initialize(*args) @named_routes = {} @generation_key_analyzer = Analysis::Frequency.new super end |
#rehash ⇒ Object
:nodoc:
91 92 93 94 95 96 |
# File 'lib/rack/mount/generation/route_set.rb', line 91 def rehash #:nodoc: @generation_keys = build_generation_keys @generation_graph = build_generation_graph super end |
#url(*args) ⇒ Object
Generates path from identifiers or significant keys.
To generate a url by named route, pass the name in as a Symbol
.
url(:dashboard) # => "/dashboard"
Additional parameters can be passed in as a hash
url(:people, :id => "1") # => "/people/1"
If no name route is given, it will fall back to a slower generation search.
url(:controller => "people", :action => "show", :id => "1")
# => "/people/1"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rack/mount/generation/route_set.rb', line 35 def url(*args) named_route, params, recall, = extract_params!(*args) [:parameterize] ||= lambda { |name, param| Utils.escape_uri(param) } unless result = generate(:path_info, named_route, params, recall, ) return end uri, params = result params.each do |k, v| if v params[k] = v else params.delete(k) end end uri << "?#{Utils.build_nested_query(params)}" if uri && params.any? uri end |