Permalink
Browse files

remove old path

  • Loading branch information...
1 parent fccf14a commit 51c39b92da6b7dee6fb16226317b3819b6be13ee @joshbuddy committed Jul 23, 2011
View
@@ -7,7 +7,6 @@
require 'http_router/request'
require 'http_router/response'
require 'http_router/route'
-require 'http_router/path'
require 'http_router/rack'
require 'http_router/regex_route'
@@ -11,7 +11,7 @@ class Node
autoload :Arbitrary, 'http_router/node/arbitrary'
autoload :Request, 'http_router/node/request'
autoload :Lookup, 'http_router/node/lookup'
- autoload :Destination, 'http_router/node/destination'
+ autoload :Path, 'http_router/node/path'
attr_reader :priority, :router, :node_position, :parent
@@ -32,6 +32,7 @@ def add_glob_regexp(matcher)
end
def add_request(opts)
+ raise unless opts
add(Request.new(@router, self, opts))
end
@@ -51,8 +52,8 @@ def add_free_match(regexp)
add(FreeRegex.new(@router, self, regexp))
end
- def add_destination(blk, partial)
- add(Destination.new(@router, self, blk, partial))
+ def add_destination(route, path, param_names = [])
+ add(Path.new(@router, self, route, path, param_names))
end
def add_lookup(part)
@@ -1,45 +0,0 @@
-class HttpRouter
- class Node
- class Destination < Node
- attr_reader :blk, :allow_partial, :param_names
-
- def initialize(router, parent, blk, allow_partial)
- @blk, @allow_partial = blk, allow_partial
- super(router, parent)
- end
-
- def usable?(other)
- other.class == self.class && other.allow_partial == allow_partial && other.blk == blk
- end
-
- def to_code
- path = blk
- path_ivar = :"@path_#{router.next_counter}"
- inject_root_ivar(path_ivar, blk)
- "#{"if request.path_finished?" unless @allow_partial}
- catch(:pass) do
- #{"if request.path.size == 1 && request.path.first == '' && (request.rack_request.head? || request.rack_request.get?) && request.rack_request.path_info[-1] == ?/
- response = ::Rack::Response.new
- response.redirect(request.rack_request.path_info[0, request.rack_request.path_info.size - 1], 302)
- throw :success, response.finish
- end" if @router.redirect_trailing_slash?}
-
- #{"if request.path.empty?#{" or (request.path.size == 1 and request.path.first == '')" if @router.ignore_trailing_slash?}" unless @allow_partial}
- if request.perform_call
- env = request.rack_request.dup.env
- env['router.request'] = request
- env['router.params'] ||= {}
- #{"env['router.params'].merge!(Hash[#{path.param_names.inspect}.zip(request.params)])" if path.dynamic?}
- #{@allow_partial ? "router.rewrite_partial_path_info(env, request)" : "router.rewrite_path_info(env, request)" }
- response = @router.process_destination_path(#{path_ivar}, env)
- router.pass_on_response(response) ? throw(:pass) : throw(:success, response)
- else
- throw :success, Response.new(request, #{path_ivar})
- end
- #{"end" unless @allow_partial}
- end
- #{"end" unless @allow_partial}"
- end
- end
- end
-end
@@ -0,0 +1,96 @@
+class HttpRouter
+ class Node
+ class Path < Node
+ attr_reader :route, :param_names, :dynamic, :original_path
+ alias_method :dynamic?, :dynamic
+ def initialize(router, parent, route, path, param_names = [])
+ @route, @original_path, @param_names, @dynamic = route, path, param_names, !param_names.empty?
+ raise AmbiguousVariableException, "You have duplicate variable name present: #{param_names.join(', ')}" if param_names.uniq.size != param_names.size
+ process_path
+ super router, parent
+ end
+
+ def hashify_params(params)
+ @dynamic && params ? Hash[param_names.zip(params)] : {}
+ end
+
+ def url(args, options)
+ if path = raw_url(args, options)
+ raise TooManyParametersException unless args.empty?
+ [URI.escape(path), options]
+ end
+ end
+
+ def to_code
+ path_ivar = :"@path_#{router.next_counter}"
+ inject_root_ivar(path_ivar, self)
+ "#{"if request.path_finished?" unless route.match_partially?}
+ catch(:pass) do
+ #{"if request.path.size == 1 && request.path.first == '' && (request.rack_request.head? || request.rack_request.get?) && request.rack_request.path_info[-1] == ?/
+ response = ::Rack::Response.new
+ response.redirect(request.rack_request.path_info[0, request.rack_request.path_info.size - 1], 302)
+ throw :success, response.finish
+ end" if router.redirect_trailing_slash?}
+
+ #{"if request.path.empty?#{" or (request.path.size == 1 and request.path.first == '')" if router.ignore_trailing_slash?}" unless route.match_partially?}
+ if request.perform_call
+ env = request.rack_request.dup.env
+ env['router.request'] = request
+ env['router.params'] ||= {}
+ #{"env['router.params'].merge!(Hash[#{param_names.inspect}.zip(request.params)])" if dynamic?}
+ router.rewrite#{"_partial" if route.match_partially?}_path_info(env, request)
+ response = @router.process_destination_path(#{path_ivar}, env)
+ router.pass_on_response(response) ? throw(:pass) : throw(:success, response)
+ else
+ throw :success, Response.new(request, #{path_ivar})
+ end
+ #{"end" unless route.match_partially?}
+ end
+ #{"end" unless route.match_partially?}"
+ end
+
+ def usable?(other)
+ other == self
+ end
+
+ private
+ def raw_url(args, options)
+ raise InvalidRouteException
+ end
+
+ def process_path
+ if @original_path.respond_to?(:split)
+ regex_parts = @original_path.split(/([:\*][a-zA-Z0-9_]+)/)
+ path_validation_regex, code = '', ''
+ regex_parts.each_with_index{ |part, index|
+ new_part = case part[0]
+ when ?:, ?*
+ if index != 0 && regex_parts[index - 1][-1] == ?\\
+ path_validation_regex << Regexp.quote(part)
+ code << part
+ else
+ path_validation_regex << (route.matches_with[part[1, part.size].to_sym] || '.*?').to_s
+ code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
+ end
+ else
+ path_validation_regex << Regexp.quote(part)
+ code << part
+ end
+ new_part
+ }
+ @path_validation_regex = Regexp.new("^#{path_validation_regex}$")
+ if @dynamic
+ instance_eval <<-EOT, __FILE__, __LINE__ + 1
+ def raw_url(args, options)
+ url = \"#{code}\"
+ #{"url !~ @path_validation_regex ? nil : " if @dynamic} url
+ end
+ EOT
+ else
+ instance_eval "def raw_url(args, options); \"#{code}\"; end", __FILE__, __LINE__
+ end
+ end
+ end
+ end
+ end
+end
@@ -1,59 +0,0 @@
-require 'uri'
-class HttpRouter
- class Path
- attr_reader :route, :param_names, :dynamic
- alias_method :dynamic?, :dynamic
- def initialize(route, path, param_names = [])
- @route, @path, @param_names, @dynamic = route, path, param_names, !param_names.empty?
- duplicate_param_names = param_names.dup.uniq!
- raise AmbiguousVariableException, "You have duplicate variable name present: #{duplicate_param_names.join(', ')}" if duplicate_param_names
- if path.respond_to?(:split)
- regex_parts = path.split(/([:\*][a-zA-Z0-9_]+)/)
- @path_validation_regex, code = '', ''
- regex_parts.each_with_index{ |part, index|
- new_part = case part[0]
- when ?:, ?*
- if index != 0 && regex_parts[index - 1][-1] == ?\\
- @path_validation_regex << Regexp.quote(part)
- code << part
- else
- @path_validation_regex << (route.matches_with[part[1, part.size].to_sym] || '.*?').to_s
- code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
- end
- else
- @path_validation_regex << Regexp.quote(part)
- code << part
- end
- new_part
- }
- @path_validation_regex = Regexp.new("^#{@path_validation_regex}$")
- instance_eval <<-EOT, __FILE__, __LINE__ + 1
- def raw_url(args, options)
- url = \"#{code}\"
- #{"url !~ @path_validation_regex ? nil : " if @dynamic} url
- end
- EOT
- end
- end
-
- def hashify_params(params)
- @dynamic && params ? Hash[param_names.zip(params)] : {}
- end
-
- def url(args, options)
- if path = raw_url(args, options)
- raise TooManyParametersException unless args.empty?
- [URI.escape(path), options]
- end
- end
-
- def original_path
- @path
- end
-
- private
- def raw_url(args, options)
- raise InvalidRouteException
- end
- end
-end
@@ -247,10 +247,9 @@ def add_path_to_tree
end
def add_non_path_to_tree(node, path, names)
- path_obj = Path.new(self, path, names)
node = node.add_request(@conditions) unless @conditions.empty?
@arbitrary.each{|a| node = node.add_arbitrary(a, match_partially?, names)} if @arbitrary
- node.add_destination(path_obj, @match_partially)
+ path_obj = node.add_destination(self, path, names)
if dest.respond_to?(:url_mount=)
urlmount = UrlMount.new(@original_path, @default_values)
urlmount.url_mount = router.url_mount if router.url_mount

0 comments on commit 51c39b9

Please sign in to comment.