lib/usher/node.rb in joshbuddy-usher-0.2.0 vs lib/usher/node.rb in joshbuddy-usher-0.2.1
- old
+ new
@@ -5,10 +5,11 @@
class Usher
class Node
ConditionalTypes = [:protocol, :domain, :port, :query_string, :remote_ip, :user_agent, :referer, :method]
+ Response = Struct.new(:path, :params)
attr_reader :lookup
attr_accessor :terminates, :exclusive_type, :parent, :value
def initialize(parent, value)
@@ -68,31 +69,31 @@
def add(route)
route.paths.each do |path|
parts = path.parts.dup
ConditionalTypes.each do |type|
- parts.push(Route::Http.new(type, route.conditions[type])) if route.conditions[type]
+ parts.push(Route::RequestMethod.new(type, route.conditions[type])) if route.conditions[type]
end
current_node = self
until parts.size.zero?
key = parts.shift
target_node = case key
- when Route::Http
+ when Route::RequestMethod
if current_node.exclusive_type == key.type
current_node.lookup[key.value] ||= Node.new(current_node, key)
elsif current_node.lookup.empty?
current_node.exclusive_type = key.type
current_node.lookup[key.value] ||= Node.new(current_node, key)
else
parts.unshift(key)
- current_node.lookup[nil] ||= Node.new(current_node, Route::Http.new(current_node.exclusive_type, nil))
+ 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::Http.new(current_node.exclusive_type, nil))
+ 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
end
current_node = target_node
@@ -111,24 +112,19 @@
ret = n.find(request, path.dup, params.dup) if n
ret and return ret
end
elsif path.size.zero? && !part
if terminates?
- [terminates, params]
+ Response.new(terminates, params)
else
nil
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)
- case t = next_part.value.transformer
- when Proc
- part = t.call(part)
- when Symbol
- part = part.send(t)
- end
- raise ValidationException.new("#{part} does not conform to #{next_part.value.validator}") if next_part.value.validator && (not next_part.value.validator === part)
+ 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
when :':'