From ad1a89164927e1d87062a350ce259b3713c9e898 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 12 Sep 2011 16:11:55 -0700 Subject: unfactor the Route class to private factory methods --- actionpack/lib/action_dispatch/routing/route.rb | 40 ------------------ .../lib/action_dispatch/routing/route_set.rb | 47 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 45 deletions(-) delete mode 100644 actionpack/lib/action_dispatch/routing/route.rb (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/route.rb b/actionpack/lib/action_dispatch/routing/route.rb deleted file mode 100644 index 76d18e4dd5..0000000000 --- a/actionpack/lib/action_dispatch/routing/route.rb +++ /dev/null @@ -1,40 +0,0 @@ -module ActionDispatch - module Routing - class Route #:nodoc: - attr_reader :conditions, :path - - def initialize(valid_conditions, app, conditions, requirements, defaults, name, anchor) - @valid_conditions = valid_conditions - strexp = Journey::Router::Strexp.new( - conditions.delete(:path_info), - requirements, - SEPARATORS, - anchor) - - @path = Journey::Path::Pattern.new(strexp) - - @verbs = conditions[:request_method] || [] - - @conditions = conditions.dup - - # Rack-Mount requires that :request_method be a regular expression. - # :request_method represents the HTTP verb that matches this route. - # - # Here we munge values before they get sent on to rack-mount. - unless @verbs.empty? - @conditions[:request_method] = %r[^#{@verbs.join('|')}$] - end - @conditions.delete_if{ |k,v| !valid_condition?(k) } - end - - def segment_keys - @segment_keys ||= path.names.compact.map { |key| key.to_sym } - end - - private - def valid_condition?(method) - segment_keys.include?(method) || @valid_conditions.include?(method) - end - end - end -end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index dad4ecb27f..26e6d68b1e 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -222,9 +222,15 @@ module ActionDispatch self.default_url_options = {} self.request_class = request_class - self.valid_conditions = request_class.public_instance_methods.map { |m| m.to_sym } + @valid_conditions = {} + + request_class.public_instance_methods.each { |m| + @valid_conditions[m.to_sym] = true + } + @valid_conditions[:controller] = true + @valid_conditions[:action] = true + self.valid_conditions.delete(:id) - self.valid_conditions.push(:controller, :action) @append = [] @prepend = [] @@ -345,13 +351,44 @@ module ActionDispatch def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i) - foo = Route.new(valid_conditions, app, conditions, requirements, defaults, name, anchor) - path = foo.path - route = @set.add_route(app, path, foo.conditions, defaults, name) + + path = build_path(conditions.delete(:path_info), requirements, SEPARATORS, anchor) + conditions = build_conditions(conditions, valid_conditions, path.names.map { |x| x.to_sym }) + + route = @set.add_route(app, path, conditions, defaults, name) named_routes[name] = route if name route end + def build_path(path, requirements, separators, anchor) + strexp = Journey::Router::Strexp.new( + path, + requirements, + SEPARATORS, + anchor) + + Journey::Path::Pattern.new(strexp) + end + private :build_path + + def build_conditions(current_conditions, req_predicates, path_values) + conditions = current_conditions.dup + + verbs = conditions[:request_method] || [] + + # Rack-Mount requires that :request_method be a regular expression. + # :request_method represents the HTTP verb that matches this route. + # + # Here we munge values before they get sent on to rack-mount. + unless verbs.empty? + conditions[:request_method] = %r[^#{verbs.join('|')}$] + end + conditions.delete_if { |k,v| !(req_predicates.include?(k) || path_values.include?(k)) } + + conditions + end + private :build_conditions + class Generator #:nodoc: PARAMETERIZE = lambda do |name, value| if name == :controller -- cgit v1.2.3