Class: ActionDispatch::Journey::Routes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/action_dispatch/journey/routes.rb

Overview

The Routing table. Contains all routes for a system. Routes can be added to the table by calling Routes#add_route.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoutes

Returns a new instance of Routes.



10
11
12
13
14
15
16
# File 'lib/action_dispatch/journey/routes.rb', line 10

def initialize
  @routes             = []
  @named_routes       = {}
  @ast                = nil
  @partitioned_routes = nil
  @simulator          = nil
end

Instance Attribute Details

#named_routesObject (readonly)

Returns the value of attribute named_routes.



8
9
10
# File 'lib/action_dispatch/journey/routes.rb', line 8

def named_routes
  @named_routes
end

#routesObject (readonly)

Returns the value of attribute routes.



8
9
10
# File 'lib/action_dispatch/journey/routes.rb', line 8

def routes
  @routes
end

Instance Method Details

#add_route(app, path, conditions, defaults, name = nil) ⇒ Object

Add a route to the routing table.



61
62
63
64
65
66
67
68
69
# File 'lib/action_dispatch/journey/routes.rb', line 61

def add_route(app, path, conditions, defaults, name = nil)
  route = Route.new(name, app, path, conditions, defaults)

  route.precedence = routes.length
  routes << route
  named_routes[name] = route if name && !named_routes[name]
  clear_cache!
  route
end

#astObject



46
47
48
49
50
51
# File 'lib/action_dispatch/journey/routes.rb', line 46

def ast
  @ast ||= begin
    asts = partitioned_routes.first.map(&:ast)
    Nodes::Or.new(asts) unless asts.empty?
  end
end

#clearObject



35
36
37
38
# File 'lib/action_dispatch/journey/routes.rb', line 35

def clear
  routes.clear
  named_routes.clear
end

#each(&block) ⇒ Object



31
32
33
# File 'lib/action_dispatch/journey/routes.rb', line 31

def each(&block)
  routes.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/action_dispatch/journey/routes.rb', line 18

def empty?
  routes.empty?
end

#lastObject



27
28
29
# File 'lib/action_dispatch/journey/routes.rb', line 27

def last
  routes.last
end

#lengthObject Also known as: size



22
23
24
# File 'lib/action_dispatch/journey/routes.rb', line 22

def length
  routes.length
end

#partitioned_routesObject



40
41
42
43
44
# File 'lib/action_dispatch/journey/routes.rb', line 40

def partitioned_routes
  @partitioned_routes ||= routes.partition do |r|
    r.path.anchored && r.ast.grep(Nodes::Symbol).all?(&:default_regexp?)
  end
end

#simulatorObject



53
54
55
56
57
58
# File 'lib/action_dispatch/journey/routes.rb', line 53

def simulator
  @simulator ||= begin
    gtg = GTG::Builder.new(ast).transition_table
    GTG::Simulator.new(gtg)
  end
end