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.

Defined Under Namespace

Classes: UrlHelper

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #exclude?, #index_by, #many?, #pluck, #sum, #without

Constructor Details

#initializeNamedRouteCollection

Returns a new instance of NamedRouteCollection.


73
74
75
76
77
78
79
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 73

def initialize
  @routes  = {}
  @path_helpers = Set.new
  @url_helpers = Set.new
  @url_helpers_module  = Module.new
  @path_helpers_module = Module.new
end

Instance Attribute Details

#path_helpers_moduleObject (readonly)

Returns the value of attribute path_helpers_module


70
71
72
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 70

def path_helpers_module
  @path_helpers_module
end

#url_helpers_moduleObject (readonly)

Returns the value of attribute url_helpers_module


70
71
72
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 70

def url_helpers_module
  @url_helpers_module
end

Instance Method Details

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


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 104

def add(name, route)
  key       = name.to_sym
  path_name = :"#{name}_path"
  url_name  = :"#{name}_url"

  if routes.key? key
    @path_helpers_module.send :undef_method, path_name
    @url_helpers_module.send  :undef_method, url_name
  end
  routes[key] = route
  define_url_helper @path_helpers_module, route, path_name, route.defaults, name, PATH
  define_url_helper @url_helpers_module,  route, url_name,  route.defaults, name, UNKNOWN

  @path_helpers << path_name
  @url_helpers << url_name
end

#clear!Object Also known as: clear


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

def clear!
  @path_helpers.each do |helper|
    @path_helpers_module.send :undef_method, helper
  end

  @url_helpers.each do |helper|
    @url_helpers_module.send  :undef_method, helper
  end

  @routes.clear
  @path_helpers.clear
  @url_helpers.clear
end

#eachObject


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

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

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


121
122
123
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 121

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

#helper_namesObject


86
87
88
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 86

def helper_names
  @path_helpers.map(&:to_s) + @url_helpers.map(&:to_s)
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)

125
126
127
128
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 125

def key?(name)
  return unless name
  routes.key? name.to_sym
end

#lengthObject


143
144
145
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 143

def length
  routes.length
end

#namesObject


139
140
141
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 139

def names
  routes.keys
end

#route_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)

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

def route_defined?(name)
  key = name.to_sym
  @path_helpers.include?(key) || @url_helpers.include?(key)
end