lib/usher/node.rb in joshbuddy-usher-0.5.4 vs lib/usher/node.rb in joshbuddy-usher-0.5.6

- old
+ new

@@ -2,11 +2,15 @@ class Usher class Node - Response = Struct.new(:path, :params, :remaining_path, :matched_path) + class Response < Struct.new(:path, :params, :remaining_path, :matched_path) + def partial_match? + !remaining_path.nil? + end + end attr_reader :normal, :greedy, :request attr_accessor :terminates, :request_method_type, :parent, :value, :request_methods def initialize(parent, value) @@ -106,19 +110,11 @@ routes.uniq! routes end def find(usher, request_object, original_path, path, params = [], position = 0) - if request_method_type - if (specific_node = request[request_object.send(request_method_type)]) && (ret = specific_node.find(usher, request_object, original_path, path.dup, params.dup, position)) - ret - elsif (general_node = request[nil]) && (ret = general_node.find(usher, request_object, original_path, path.dup, params.dup, position)) - ret - else - nil - end - elsif terminates? && (path.empty? || terminates.route.partial_match?) + if terminates? && (path.empty? || terminates.route.partial_match?) terminates.route.partial_match? ? Response.new(terminates, params, original_path[position, original_path.size], original_path[0, position]) : Response.new(terminates, params, nil, original_path) elsif !path.empty? && (greedy? && (match_with_result_output = greedy.match_with_result(whole_path = original_path[position, original_path.size]))) next_path, matched_part = match_with_result_output @@ -128,11 +124,11 @@ elsif !path.empty? && normal && (next_part = normal[part = path.shift] || normal[nil]) position += part.size case next_part.value when Route::Variable::Glob params << [next_part.value.name, []] unless params.last && params.last.first == next_part.value.name - loop do + while true if (next_part.value.look_ahead === part || (!usher.delimiter_chars.include?(part[0]) && next_part.value.regex_matcher && !next_part.value.regex_matcher.match(part))) path.unshift(part) position -= part.size if usher.delimiter_chars.include?(next_part.parent.value[0]) path.unshift(next_part.parent.value) @@ -151,16 +147,24 @@ end when Route::Variable::Single var = next_part.value var.valid!(part) params << [var.name, part] - until (var.look_ahead === path.first) || path.empty? + until path.empty? || (var.look_ahead === path.first) next_path_part = path.shift position += next_path_part.size params.last.last << next_path_part - end + end if var.look_ahead && usher.delimiter_chars.size > 1 end next_part.find(usher, request_object, original_path, path, params, position) + elsif request_method_type + if (specific_node = request[request_object.send(request_method_type)]) && (ret = specific_node.find(usher, request_object, original_path, path.dup, params.dup, position)) + ret + elsif (general_node = request[nil]) && (ret = general_node.find(usher, request_object, original_path, path.dup, params.dup, position)) + ret + else + nil + end else nil end end