Skip to content

Commit

Permalink
pass match up to avoid having to re-do work
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Jun 11, 2010
1 parent 04e7812 commit f6ad5fd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/http_router/glob.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class HttpRouter
class Glob < Variable
def matches?(parts)
@matches_with.nil? or (!parts.empty? and match = @matches_with.match(parts.first) and match.begin(0))
@matches_with.nil? or (!parts.empty? and match = @matches_with.match(parts.first) and match.begin(0)) ? match : nil
end

def consume(parts)
def consume(match, parts)
if @matches_with
params = [parts.shift]
params << parts.shift while matches?(parts)
Expand Down
6 changes: 3 additions & 3 deletions lib/http_router/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def find_on_parts(request, parts, params)
response = nil
dupped_parts = nil
next_node = @linear.find do |(tester, node)|
if tester.respond_to?(:matches?) and tester.matches?(parts)
if tester.respond_to?(:matches?) and match = tester.matches?(parts)
dupped_parts = parts.dup
params << tester.consume(dupped_parts)
params << tester.consume(match, dupped_parts)
parts.replace(dupped_parts) if response = node.find_on_parts(request, dupped_parts, params)
elsif tester.respond_to?(:match) and match = tester.match(parts.whole_path) and match.begin(0) == 0
dupped_parts = router.split(parts.whole_path[match[0].size, parts.whole_path.size])
Expand All @@ -167,7 +167,7 @@ def find_on_parts(request, parts, params)
parts.shift
return match.find_on_parts(request, parts, params)
elsif @catchall
params << @catchall.variable.consume(parts)
params << @catchall.variable.consume(nil, parts)
return @catchall.find_on_parts(request, parts, params)
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/http_router/variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ def initialize(router, name, matches_with = nil)
end

def matches?(parts)
@matches_with.nil? or (@matches_with and match = @matches_with.match(parts.whole_path) and match.begin(0) == 0)
@matches_with.nil? or (@matches_with and match = @matches_with.match(parts.whole_path) and match.begin(0) == 0) ? match : nil
end

def consume(parts)
def consume(match, parts)
if @matches_with
match = @matches_with.match(parts.whole_path)
parts.replace(router.split(parts.whole_path[match.end(0), parts.whole_path.size]))
match[0]
else
Expand Down

0 comments on commit f6ad5fd

Please sign in to comment.