Skip to content

Commit

Permalink
unfactor the Route class to private factory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 12, 2011
1 parent 943aa82 commit ad1a891
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
40 changes: 0 additions & 40 deletions actionpack/lib/action_dispatch/routing/route.rb

This file was deleted.

47 changes: 42 additions & 5 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -222,9 +222,15 @@ def initialize(request_class = ActionDispatch::Request)
self.default_url_options = {} self.default_url_options = {}


self.request_class = request_class 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.delete(:id)
self.valid_conditions.push(:controller, :action)


@append = [] @append = []
@prepend = [] @prepend = []
Expand Down Expand Up @@ -345,13 +351,44 @@ def empty?


def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) 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) 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 path = build_path(conditions.delete(:path_info), requirements, SEPARATORS, anchor)
route = @set.add_route(app, path, foo.conditions, defaults, name) 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 named_routes[name] = route if name
route route
end 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: class Generator #:nodoc:
PARAMETERIZE = lambda do |name, value| PARAMETERIZE = lambda do |name, value|
if name == :controller if name == :controller
Expand Down

0 comments on commit ad1a891

Please sign in to comment.