lib/usher/node.rb in joshbuddy-usher-0.3.2 vs lib/usher/node.rb in joshbuddy-usher-0.3.3
- old
+ new
@@ -14,11 +14,10 @@
def initialize(parent, value)
@parent = parent
@value = value
@lookup = FuzzyHash.new
@exclusive_type = nil
- @has_globber = find_parent{|p| p.value && p.value.is_a?(Route::Variable)}
end
def depth
@depth ||= @parent && @parent.is_a?(Node) ? @parent.depth + 1 : 0
end
@@ -27,32 +26,14 @@
root = self.new(route_set, nil)
root.request_methods = request_methods
root
end
- def has_globber?
- @has_globber
- end
-
def terminates?
@terminates
end
- def find_parent(&blk)
- if @parent.nil? || !@parent.is_a?(Node)
- nil
- elsif yield @parent
- @parent
- else #keep searching
- @parent.find_parent(&blk)
- end
- end
-
- def replace(src, dest)
- @lookup.replace(src, dest)
- end
-
def pp
$stdout << " " * depth
$stdout << "#{depth}: #{value.inspect} #{!!terminates?}\n"
@lookup.each do |k,v|
$stdout << " " * (depth + 1)
@@ -81,16 +62,11 @@
else
parts.unshift(key)
current_node.lookup[nil] ||= Node.new(current_node, Route::RequestMethod.new(current_node.exclusive_type, nil))
end
else
- if current_node.exclusive_type
- parts.unshift(key)
- current_node.lookup[nil] ||= Node.new(current_node, Route::RequestMethod.new(current_node.exclusive_type, nil))
- else
- current_node.lookup[key.is_a?(Route::Variable) ? nil : key] ||= Node.new(current_node, key)
- end
+ current_node.lookup[key.is_a?(Route::Variable) ? nil : key] ||= Node.new(current_node, key)
end
current_node = target_node
end
current_node.terminates = path
end
@@ -107,30 +83,28 @@
ret and return ret
end
elsif path.size.zero? && !part
if terminates?
Response.new(terminates, params)
- else
- nil
+ elsif params.last.is_a?(Array) && @lookup[nil]
+ Response.new(@lookup[nil].terminates, params)
end
elsif next_part = @lookup[part]
next_part.find(request, path, params)
elsif next_part = @lookup[nil]
if next_part.value.is_a?(Route::Variable)
part = next_part.value.transform!(part)
next_part.value.valid!(part)
case next_part.value.type
when :*
- params << [next_part.value.name, []]
- params.last.last << part
+ params << [next_part.value.name, []] unless params.last && params.last.first == next_part.value.name
+ params.last.last << part unless part.is_a?(Symbol)
+ find(request, path, params)
when :':'
params << [next_part.value.name, part]
+ next_part.find(request, path, params)
end
end
- next_part.find(request, path, params)
- elsif has_globber? && p = find_parent{|p| p.value.is_a?(Route::Variable) && p.value.type == :*}
- params.last.last << part
- find(request, path, params)
else
nil
end
end