Permalink
Browse files

reduce dependencies of external objects in the Route class

  • Loading branch information...
1 parent f5ab680 commit 943aa826a48c7db682e24f13cc93ed3298aa7e1a @tenderlove tenderlove committed Sep 12, 2011
@@ -3,10 +3,15 @@ module Routing
class Route #:nodoc:
attr_reader :conditions, :path
- def initialize(set, app, conditions, requirements, defaults, name, anchor)
- @set = set
- path = ::Rack::Mount::Strexp.new(
- conditions[:path_info], requirements, SEPARATORS, anchor)
+ 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] || []
@@ -16,22 +21,19 @@ def initialize(set, app, conditions, requirements, defaults, name, anchor)
# :request_method represents the HTTP verb that matches this route.
#
# Here we munge values before they get sent on to rack-mount.
- @conditions[:request_method] = %r[^#{verb}$] unless @verbs.empty?
- @conditions[:path_info] = Rack::Mount::RegexpWithNamedGroups.new(path)
- @conditions.delete_if{ |k,v| k != :path_info && !valid_condition?(k) }
- end
-
- def verb
- @verbs.join '|'
+ unless @verbs.empty?
+ @conditions[:request_method] = %r[^#{@verbs.join('|')}$]
+ end
+ @conditions.delete_if{ |k,v| !valid_condition?(k) }
end
def segment_keys
- @segment_keys ||= conditions[:path_info].names.compact.map { |key| key.to_sym }
+ @segment_keys ||= path.names.compact.map { |key| key.to_sym }
end
private
def valid_condition?(method)
- segment_keys.include?(method) || @set.valid_conditions.include?(method)
+ segment_keys.include?(method) || @valid_conditions.include?(method)
end
end
end
@@ -345,9 +345,9 @@ 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)
- route = Route.new(self, app, conditions, requirements, defaults, name, anchor)
- path = route.conditions[:path_info]
- route = @set.add_route(app, path, route.conditions, defaults, name)
+ foo = Route.new(valid_conditions, app, conditions, requirements, defaults, name, anchor)
+ path = foo.path
+ route = @set.add_route(app, path, foo.conditions, defaults, name)
named_routes[name] = route if name
route
end

0 comments on commit 943aa82

Please sign in to comment.