Module: Rack::Mount::Generation::RouteSet

Included in:
RouteSet
Defined in:
lib/rack/mount/generation/route_set.rb

Constant Summary collapse

DEFAULT_KEYS =

[:controller, :action].freeze

[]

Instance Method Summary collapse

Instance Method Details

#add_route(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/rack/mount/generation/route_set.rb', line 14

def add_route(*args)
  route = super

  @named_routes[route.name] = route if route.name

  keys = @generation_keys.map { |key| route.defaults[key] }
  @generation_graph[*keys] = route

  route
end

#freezeObject



51
52
53
54
55
56
# File 'lib/rack/mount/generation/route_set.rb', line 51

def freeze
  @named_routes.freeze
  @generation_keys.freeze
  @generation_graph.freeze
  super
end

#initialize(options = {}) ⇒ Object



7
8
9
10
11
12
# File 'lib/rack/mount/generation/route_set.rb', line 7

def initialize(options = {})
  @named_routes = {}
  @generation_keys = DEFAULT_KEYS
  @generation_graph = NestedSet.new
  super
end

#url_for(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack/mount/generation/route_set.rb', line 25

def url_for(*args)
  params = args.last.is_a?(Hash) ? args.pop : {}
  named_route = args.shift
  route = nil

  if named_route
    unless route = @named_routes[named_route.to_sym]
      raise RoutingError, "#{named_route} failed to generate from #{params.inspect}"
    end
  else
    keys = @generation_keys.map { |key| params[key] }
    @generation_graph[*keys].each do |r|
      if r.defaults.all? { |k, v| params[k] == v }
        route = r
        break
      end
    end

    unless route
      raise RoutingError, "No route matches #{params.inspect}"
    end
  end

  route.url_for(params)
end