Class: ActionDispatch::Routing::RouteSet::NamedRouteCollection
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::RouteSet::NamedRouteCollection
- Includes:
- Enumerable
- Defined in:
- 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
-
#path_helpers_module ⇒ Object
readonly
Returns the value of attribute path_helpers_module.
-
#url_helpers_module ⇒ Object
readonly
Returns the value of attribute url_helpers_module.
Instance Method Summary collapse
- #add(name, route) ⇒ Object (also: #[]=)
-
#add_url_helper(name, defaults, &block) ⇒ Object
Given a
name
, defines name_path and name_url helpers. - #clear! ⇒ Object (also: #clear)
- #each(&block) ⇒ Object
- #get(name) ⇒ Object (also: #[])
- #helper_names ⇒ Object
-
#initialize ⇒ NamedRouteCollection
constructor
A new instance of NamedRouteCollection.
- #key?(name) ⇒ Boolean
- #length ⇒ Object
- #names ⇒ Object
- #route_defined?(name) ⇒ Boolean
Constructor Details
#initialize ⇒ NamedRouteCollection
Returns a new instance of NamedRouteCollection.
71 72 73 74 75 76 77 |
# File 'lib/action_dispatch/routing/route_set.rb', line 71 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_module ⇒ Object (readonly)
Returns the value of attribute path_helpers_module.
68 69 70 |
# File 'lib/action_dispatch/routing/route_set.rb', line 68 def path_helpers_module @path_helpers_module end |
#url_helpers_module ⇒ Object (readonly)
Returns the value of attribute url_helpers_module.
68 69 70 |
# File 'lib/action_dispatch/routing/route_set.rb', line 68 def url_helpers_module @url_helpers_module end |
Instance Method Details
#add(name, route) ⇒ Object Also known as: []=
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/action_dispatch/routing/route_set.rb', line 102 def add(name, route) key = name.to_sym path_name = :"#{name}_path" url_name = :"#{name}_url" if routes.key? key @path_helpers_module.undef_method path_name @url_helpers_module.undef_method url_name end routes[key] = route helper = UrlHelper.create(route, route.defaults, name) define_url_helper @path_helpers_module, path_name, helper, PATH define_url_helper @url_helpers_module, url_name, helper, UNKNOWN @path_helpers << path_name @url_helpers << url_name end |
#add_url_helper(name, defaults, &block) ⇒ Object
Given a name
, defines name_path and name_url helpers. Used by ‘direct’, ‘resolve’, and ‘polymorphic’ route helpers.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/action_dispatch/routing/route_set.rb', line 149 def add_url_helper(name, defaults, &block) helper = CustomUrlHelper.new(name, defaults, &block) path_name = :"#{name}_path" url_name = :"#{name}_url" @path_helpers_module.module_eval do redefine_method(path_name) do |*args| helper.call(self, args, true) end end @url_helpers_module.module_eval do redefine_method(url_name) do |*args| helper.call(self, args, false) end end @path_helpers << path_name @url_helpers << url_name self end |
#clear! ⇒ Object Also known as: clear
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/action_dispatch/routing/route_set.rb', line 88 def clear! @path_helpers.each do |helper| @path_helpers_module.remove_method helper end @url_helpers.each do |helper| @url_helpers_module.remove_method helper end @routes.clear @path_helpers.clear @url_helpers.clear end |
#each(&block) ⇒ Object
134 135 136 137 |
# File 'lib/action_dispatch/routing/route_set.rb', line 134 def each(&block) routes.each(&block) self end |
#get(name) ⇒ Object Also known as: []
121 122 123 |
# File 'lib/action_dispatch/routing/route_set.rb', line 121 def get(name) routes[name.to_sym] end |
#helper_names ⇒ Object
84 85 86 |
# File 'lib/action_dispatch/routing/route_set.rb', line 84 def helper_names @path_helpers.map(&:to_s) + @url_helpers.map(&:to_s) end |
#key?(name) ⇒ Boolean
125 126 127 128 |
# File 'lib/action_dispatch/routing/route_set.rb', line 125 def key?(name) return unless name routes.key? name.to_sym end |
#length ⇒ Object
143 144 145 |
# File 'lib/action_dispatch/routing/route_set.rb', line 143 def length routes.length end |
#names ⇒ Object
139 140 141 |
# File 'lib/action_dispatch/routing/route_set.rb', line 139 def names routes.keys end |
#route_defined?(name) ⇒ Boolean
79 80 81 82 |
# File 'lib/action_dispatch/routing/route_set.rb', line 79 def route_defined?(name) key = name.to_sym @path_helpers.include?(key) || @url_helpers.include?(key) end |