123456789_123456789_123456789_123456789_123456789_

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

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
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.

Class Method Summary

Instance Attribute Summary

::Enumerable - Included

#many?

Returns true if the enumerable has more than 1 element.

Instance Method Summary

::Enumerable - Included

#compact_blank

Returns a new ::Array without the blank items.

#exclude?

The negative of the Enumerable#include?.

#excluding

Returns a copy of the enumerable excluding the specified elements.

#in_order_of

Returns a new ::Array where the order has been set to that provided in the series, based on the key of the objects in the original enumerable.

#including

Returns a new array that includes the passed elements.

#index_by

Convert an enumerable to a hash, using the block result as the key and the element as the value.

#index_with

Convert an enumerable to a hash, using the element as the key and the block result as the value.

#maximum

Calculates the maximum from the extracted elements.

#minimum

Calculates the minimum from the extracted elements.

#pick

Extract the given key from the first element in the enumerable.

#pluck

Extract the given key from each element in the enumerable.

#sole

Returns the sole item in the enumerable.

#without
#as_json

::ActiveSupport::EnumerableCoreExt::Constants - Included

Constructor Details

.newNamedRouteCollection

[ GitHub ]

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

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 (readonly)

[ GitHub ]

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

attr_reader :routes, :url_helpers_module, :path_helpers_module

#routes (readonly, private)

[ GitHub ]

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

attr_reader :routes, :url_helpers_module, :path_helpers_module

#url_helpers_module (readonly)

[ GitHub ]

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

attr_reader :routes, :url_helpers_module, :path_helpers_module

Instance Method Details

#[](name)

Alias for #get.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 149

alias []    get

#[]=(name, route)

Alias for #add.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 148

alias []=   add

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

[ GitHub ]

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

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)

Given a name, defines name_path and name_url helpers. Used by ‘direct’, ‘resolve’, and ‘polymorphic’ route helpers.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 167

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

Alias for #clear!.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 150

alias clear clear!

#clear! Also known as: #clear

[ GitHub ]

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

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

#define_url_helper(mod, name, helper, url_strategy) (private)

Create a URL helper allowing ordered parameters to be associated with corresponding dynamic segments, so you can do:

foo_url(bar, baz, bang)

Instead of:

foo_url(bar: bar, baz: baz, bang: bang)

Also allow options hash, so you can do:

foo_url(bar, baz, bang, sort_by: 'baz')
[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 335

def define_url_helper(mod, name, helper, url_strategy)
  mod.define_method(name) do |*args|
    last = args.last
    options = \
      case last
      when Hash
        args.pop
      when ActionController::Parameters
        args.pop.to_h
      end
    helper.call(self, name, args, options, url_strategy)
  end
end

#each(&block)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 152

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

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

[ GitHub ]

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

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

#helper_names

[ GitHub ]

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

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

#key?(name) ⇒ Boolean

[ GitHub ]

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

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

#length

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 161

def length
  routes.length
end

#names

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 157

def names
  routes.keys
end

#route_defined?(name) ⇒ Boolean

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 97

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