Skip to content
Browse files

unfactor the Route class to private factory methods

  • Loading branch information...
1 parent 943aa82 commit ad1a89164927e1d87062a350ce259b3713c9e898 @tenderlove tenderlove committed Sep 12, 2011
View
40 actionpack/lib/action_dispatch/routing/route.rb
@@ -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
View
47 actionpack/lib/action_dispatch/routing/route_set.rb
@@ -222,9 +222,15 @@ def initialize(request_class = ActionDispatch::Request)
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 @@ def empty?
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

0 comments on commit ad1a891

Please sign in to comment.
Something went wrong with that request. Please try again.