Class: ActionDispatch::Routing::RouteSet::NamedRouteCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
actionpack/lib/action_dispatch/routing/route_set.rb

Overview

A NamedRouteCollection instance is a collection of named routes, and also maintains an anonymous module that can be used to install helpers for the named routes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #each_with_object, #exclude?, #group_by, #index_by, #many?, #sum

Constructor Details

#initializeNamedRouteCollection

Returns a new instance of NamedRouteCollection.



84
85
86
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 84

def initialize
  clear!
end

Instance Attribute Details

#helpersObject (readonly)

Returns the value of attribute helpers



82
83
84
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 82

def helpers
  @helpers
end

#moduleObject (readonly)

Returns the value of attribute module



82
83
84
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 82

def module
  @module
end

#routesObject (readonly)

Returns the value of attribute routes



82
83
84
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 82

def routes
  @routes
end

Instance Method Details

#add(name, route) ⇒ Object Also known as: []=



101
102
103
104
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 101

def add(name, route)
  routes[name.to_sym] = route
  define_named_route_methods(name, route)
end

#clear!Object Also known as: clear



92
93
94
95
96
97
98
99
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 92

def clear!
  @routes = {}
  @helpers = []

  @module ||= Module.new do
    instance_methods.each { |selector| remove_method(selector) }
  end
end

#eachObject



114
115
116
117
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 114

def each
  routes.each { |name, route| yield name, route }
  self
end

#get(name) ⇒ Object Also known as: []



106
107
108
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 106

def get(name)
  routes[name.to_sym]
end

#helper_namesObject



88
89
90
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 88

def helper_names
  self.module.instance_methods.map(&:to_s)
end

#install(destinations = [ActionController::Base, ActionView::Base], regenerate = false) ⇒ Object



135
136
137
138
139
140
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 135

def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false)
  reset! if regenerate
  Array(destinations).each do |dest|
    dest.__send__(:include, @module)
  end
end

#lengthObject



123
124
125
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 123

def length
  routes.length
end

#namesObject



119
120
121
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 119

def names
  routes.keys
end

#reset!Object



127
128
129
130
131
132
133
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 127

def reset!
  old_routes = routes.dup
  clear!
  old_routes.each do |name, route|
    add(name, route)
  end
end