http_router 0.3.16 → 0.3.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -45,13 +45,25 @@ class HttpRouter
45
45
 
46
46
  def add_to_linear(val)
47
47
  create_linear
48
- if @linear.assoc(val)
48
+ n = if @linear.assoc(val)
49
49
  @linear.assoc(val).last
50
50
  else
51
51
  new_node = router.node
52
52
  @linear << [val, new_node]
53
53
  new_node
54
54
  end
55
+ @linear.sort!{|a, b|
56
+ if a.first.respond_to?(:priority) and b.first.respond_to?(:priority)
57
+ b.first.priority <=> a.first.priority
58
+ elsif a.first.respond_to?(:priority)
59
+ -1
60
+ elsif b.first.respond_to?(:priority)
61
+ 1
62
+ else
63
+ 0
64
+ end
65
+ }
66
+ n
55
67
  end
56
68
 
57
69
  def add_arbitrary(procs)
@@ -258,6 +258,10 @@ class HttpRouter
258
258
  mount_point.nil? ? result : File.join(mount_point, result)
259
259
  end
260
260
 
261
+ def significant_variable_names
262
+ @significant_variable_names ||= @path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
263
+ end
264
+
261
265
  private
262
266
 
263
267
  attr_reader :router
@@ -336,15 +340,12 @@ class HttpRouter
336
340
  end
337
341
  end
338
342
 
339
- def significant_variable_names
340
- @significant_variable_names ||= @path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
341
- end
342
-
343
343
  def generate_interstitial_parts(part)
344
344
  part_segments = part.scan(/:[a-zA-Z_0-9]+|[^:]+/)
345
+ priority = 0
345
346
  if part_segments.size > 1
346
347
  index = 0
347
- part_segments.map do |seg|
348
+ segs = part_segments.map do |seg|
348
349
  new_seg = if seg[0] == ?:
349
350
  next_index = index + 1
350
351
  v_name = seg[1, seg.size].to_sym
@@ -352,8 +353,9 @@ class HttpRouter
352
353
  scan_regex = if next_index == part_segments.size
353
354
  matcher || /^[^\/]+/
354
355
  else
355
- /^#{matcher || '[^\/]*?'}(?=#{Regexp.quote(part_segments[next_index])})/
356
+ /^#{matcher || '[^\/]+?'}(?=#{Regexp.quote(part_segments[next_index])})/
356
357
  end
358
+ priority += 1
357
359
  router.variable(v_name, scan_regex)
358
360
  else
359
361
  /^#{Regexp.quote(seg)}/
@@ -361,6 +363,8 @@ class HttpRouter
361
363
  index += 1
362
364
  new_seg
363
365
  end
366
+ segs.each { |seg| seg.priority = priority if seg.respond_to?(:priority=) }
367
+ segs
364
368
  else
365
369
  part
366
370
  end
@@ -1,11 +1,13 @@
1
1
  class HttpRouter
2
2
  class Variable
3
3
  attr_reader :name, :matches_with
4
+ attr_accessor :priority
4
5
 
5
6
  def initialize(router, name, matches_with = nil)
6
7
  @router = router
7
8
  @name = name
8
9
  @matches_with = matches_with
10
+ @priority = 0
9
11
  end
10
12
 
11
13
  def matches?(parts)
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  class HttpRouter #:nodoc
3
- VERSION = '0.3.16'
3
+ VERSION = '0.3.17'
4
4
  end
@@ -383,6 +383,20 @@ describe "HttpRouter#recognize" do
383
383
  response.params_as_hash[:greed].should == 'greedyboy'
384
384
  end
385
385
 
386
+ it "should distinguish between very similar looking routes" do
387
+ @router.add('/:var1').to(:test1)
388
+ @router.add('/:var1-:var2').to(:test2)
389
+ @router.add('/:var1-:var2-:var3').to(:test3)
390
+ @router.add('/:var1-:var2-:var3-:var4').to(:test4)
391
+ @router.add('/:var1-:var2-:var3-:var4-:var5').to(:test5)
392
+ @router.add('/:var1-:var2-:var3-:var4-:var5-:var6').to(:test6)
393
+ @router.recognize(Rack::MockRequest.env_for('/one')).dest.should == :test1
394
+ @router.recognize(Rack::MockRequest.env_for('/one-value')).dest.should == :test2
395
+ @router.recognize(Rack::MockRequest.env_for('/one-value-time')).dest.should == :test3
396
+ @router.recognize(Rack::MockRequest.env_for('/one-value-time-one')).dest.should == :test4
397
+ @router.recognize(Rack::MockRequest.env_for('/one-value-time-one-variable')).dest.should == :test5
398
+ @router.recognize(Rack::MockRequest.env_for('/one-value-time-one-value-time')).dest.should == :test6
399
+ end
386
400
  end
387
401
 
388
402
  context("with dynamic greedy paths") do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_router
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 49
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 16
10
- version: 0.3.16
9
+ - 17
10
+ version: 0.3.17
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-07 00:00:00 -04:00
18
+ date: 2010-09-08 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency