Class: ActionController::Routing::RouteSet::NamedRouteCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/action_controller/routing.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

Constructor Details

#initializeNamedRouteCollection

Returns a new instance of NamedRouteCollection.



1010
1011
1012
# File 'lib/action_controller/routing.rb', line 1010

def initialize
  clear!
end

Instance Attribute Details

#helpersObject (readonly)

Returns the value of attribute helpers.



1008
1009
1010
# File 'lib/action_controller/routing.rb', line 1008

def helpers
  @helpers
end

#routesObject (readonly)

Returns the value of attribute routes.



1008
1009
1010
# File 'lib/action_controller/routing.rb', line 1008

def routes
  @routes
end

Instance Method Details

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



1024
1025
1026
1027
# File 'lib/action_controller/routing.rb', line 1024

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

#clear!Object Also known as: clear



1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/action_controller/routing.rb', line 1014

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

  @module ||= Module.new
  @module.instance_methods.each do |selector|
    @module.send :remove_method, selector
  end
end

#define_deprecated_named_route_methods(name, deprecated_name) ⇒ Object



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
# File 'lib/action_controller/routing.rb', line 1054

def define_deprecated_named_route_methods(name, deprecated_name)

  [:url, :path].each do |kind|
    @module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks

      def #{url_helper_name(deprecated_name, kind)}(*args)

        ActiveSupport::Deprecation.warn(
          'The named route "#{url_helper_name(deprecated_name, kind)}" uses a format that has been deprecated. ' +
          'You should use "#{url_helper_name(name, kind)}" instead.', caller
        )

        send :#{url_helper_name(name, kind)}, *args

      end

      def #{hash_access_name(deprecated_name, kind)}(*args)

        ActiveSupport::Deprecation.warn(
          'The named route "#{hash_access_name(deprecated_name, kind)}" uses a format that has been deprecated. ' +
          'You should use "#{hash_access_name(name, kind)}" instead.', caller
        )

        send :#{hash_access_name(name, kind)}, *args

      end

    end_eval
  end

end

#eachObject



1037
1038
1039
1040
# File 'lib/action_controller/routing.rb', line 1037

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

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



1029
1030
1031
# File 'lib/action_controller/routing.rb', line 1029

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

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



1050
1051
1052
# File 'lib/action_controller/routing.rb', line 1050

def install(destinations = [ActionController::Base, ActionView::Base])
  Array(destinations).each { |dest| dest.send :include, @module }
end

#lengthObject



1046
1047
1048
# File 'lib/action_controller/routing.rb', line 1046

def length
  routes.length
end

#namesObject



1042
1043
1044
# File 'lib/action_controller/routing.rb', line 1042

def names
  routes.keys
end