Permalink
Browse files

param_names come from root now

  • Loading branch information...
1 parent a470c30 commit 077d9b6039a7ec9d04f3665facbcb0d1506fb12a @joshbuddy committed Sep 14, 2011
Showing with 17 additions and 23 deletions.
  1. +5 −15 lib/http_router/generator.rb
  2. +12 −8 lib/http_router/node/root.rb
@@ -7,14 +7,7 @@ class PathGenerator
def initialize(route, path, validation_regex = nil)
@route = route
@path = path.dup
- @param_names = if @path.respond_to?(:names)
- @path.names.map(&:to_sym)
- elsif path.is_a?(String)
- @path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
- else
- []
- end
-
+ @param_names = []
if path.is_a?(String)
path[0, 0] = '/' unless path[0] == ?/
regex_parts = path.split(/([:\*][a-zA-Z0-9_]+)/)
@@ -56,25 +49,24 @@ def generate(args, options)
end
end
- attr_reader :param_names
def initialize(route, paths)
@route, @paths = route, paths
@router = @route.router
@route.generator = self
@path_generators = @paths.map do |p|
generator = PathGenerator.new(route, p.is_a?(String) ? p : route.path_for_generation, p.is_a?(Regexp) ? p : nil)
end
- @path_generators.sort! do |p1, p2|
- p2.param_names.size <=> p1.param_names.size
- end
end
def max_param_count
@max_param_count ||= @path_generators.map{|p| p.param_names.size}.max
end
def each_path
- @path_generators.each {|p| yield p.path, p.param_names}
+ @path_generators.each {|p| yield p }
+ @path_generators.sort! do |p1, p2|
+ p2.param_names.size <=> p1.param_names.size
+ end
end
def url(*args)
@@ -128,8 +120,6 @@ def matching_path(params, other_hash = nil)
significant_key_count += (path.param_names & other_hash.keys).size if other_hash
significant_key_count >= path.param_names.size
end
- # params_size = params ? params.size : 0
- # path.param_names.size == (significant_keys ? (params_size) + significant_keys.size : params_size) }
when Hash
@path_generators.find { |path| (params && !params.empty? && (path.param_names & params.keys).size == path.param_names.size) || path.param_names.empty? }
end
@@ -69,38 +69,41 @@ def add_route(route)
if paths.empty?
add_non_path_to_tree(route, @router.root, nil, [])
else
- Generator.new(route, paths).each_path do |path, param_names|
- case path
+ Generator.new(route, paths).each_path do |path_generator|
+ case path_generator.path
when Regexp
- add_non_path_to_tree(route, add_free_match(path), path, param_names)
+ path_generator.param_names = path_generator.path.names.map(&:to_sym) if path_generator.path.respond_to?(:names)
+ add_non_path_to_tree(route, add_free_match(path_generator.path), path_generator.path, path_generator.param_names)
else
node = self
- path.split(/\//).each do |part|
+ path_generator.path.split(/\//).each do |part|
next if part == ''
parts = part.scan(/\\.|[:*][a-z0-9_]+|[^:*\\]+/)
- node = parts.size == 1 ? add_normal_part(route, node, part) : add_complex_part(route, node, parts)
+ node = parts.size == 1 ? add_normal_part(route, node, part, path_generator) : add_complex_part(route, node, parts, path_generator)
end
- add_non_path_to_tree(route, node, path, param_names)
+ add_non_path_to_tree(route, node, path_generator.path, path_generator.param_names)
end
end
end
end
- def add_normal_part(route, node, part)
+ def add_normal_part(route, node, part, path_generator)
name = part[1, part.size]
node = case part[0]
when ?\\
node.add_lookup(part[1].chr)
when ?:
+ path_generator.param_names << name.to_sym
route.matches_with(name) ? node.add_spanning_match(route.matches_with(name)) : node.add_variable
when ?*
+ path_generator.param_names << name.to_sym
route.matches_with(name) ? node.add_glob_regexp(route.matches_with(name)) : node.add_glob
else
node.add_lookup(part)
end
end
- def add_complex_part(route, node, parts)
+ def add_complex_part(route, node, parts, path_generator)
capturing_indicies, splitting_indicies, captures, spans = [], [], 0, false
regex = parts.inject('') do |reg, part|
reg << case part[0]
@@ -110,6 +113,7 @@ def add_complex_part(route, node, parts)
captures += 1
(part[0] == ?* ? splitting_indicies : capturing_indicies) << captures
name = part[1, part.size].to_sym
+ path_generator.param_names << name.to_sym
if spans
route.matches_with(name) ? "((?:#{route.matches_with(name)}\\/?)+)" : '(.*?)'
else

0 comments on commit 077d9b6

Please sign in to comment.