joshbuddy-usher 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/usher/grapher.rb CHANGED
@@ -12,6 +12,7 @@ class Usher
12
12
  @significant_keys = nil
13
13
  @orders = Hash.new{|h,k| h[k] = Hash.new{|h2, k2| h2[k2] = []}}
14
14
  @key_count = Hash.new(0)
15
+ @cache = {}
15
16
  end
16
17
 
17
18
  def add_route(route)
data/lib/usher/node.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
- require 'node/lookup'
3
+ require 'fuzzy_hash'
4
4
 
5
5
  class Usher
6
6
 
@@ -14,7 +14,7 @@ class Usher
14
14
  def initialize(parent, value)
15
15
  @parent = parent
16
16
  @value = value
17
- @lookup = Lookup.new
17
+ @lookup = FuzzyHash.new
18
18
  @exclusive_type = nil
19
19
  @has_globber = find_parent{|p| p.value && p.value.is_a?(Route::Variable)}
20
20
  end
@@ -104,6 +104,7 @@ class Usher
104
104
 
105
105
  def find(request, path = Route::Splitter.url_split(request.path), params = [])
106
106
  part = path.shift unless path.size.zero?
107
+
107
108
  if @exclusive_type
108
109
  path.unshift part
109
110
  [@lookup[request.send(@exclusive_type)], @lookup[nil]].each do |n|
@@ -126,19 +127,21 @@ class Usher
126
127
  when Symbol
127
128
  part = part.send(t)
128
129
  end
129
- part =
130
130
  raise "#{part} does not conform to #{next_part.value.validator}" if next_part.value.validator && (not next_part.value.validator === part)
131
131
  case next_part.value.type
132
132
  when :*
133
133
  params << [next_part.value.name, []]
134
- params.last.last << part unless next_part.is_a?(Route::Separator)
134
+ params.last.last << part
135
135
  when :':'
136
136
  params << [next_part.value.name, part]
137
+ when:'.:'
138
+ part.slice!(0)
139
+ params << [next_part.value.name, part]
137
140
  end
138
141
  end
139
142
  next_part.find(request, path, params)
140
- elsif has_globber? && p = find_parent{|p| !p.is_a?(Route::Separator)} && p.value.is_a?(Route::Variable) && p.value.type == :*
141
- params.last.last << part unless part.is_a?(Route::Separator)
143
+ elsif has_globber? && p = find_parent{|p| p.value.is_a?(Route::Variable) && p.value.type == :*}
144
+ params.last.last << part
142
145
  find(request, path, params)
143
146
  else
144
147
  nil
@@ -2,8 +2,8 @@ class Usher
2
2
  class Route
3
3
  class Splitter
4
4
 
5
- ScanRegex = /([:\*]?[0-9a-z_]+|\/|\.|\(|\))/
6
- UrlScanRegex = /\/|\.|\w+/
5
+ ScanRegex = /((:|\*||\.:|)[0-9a-z_]+|\/|\(|\))/
6
+ UrlScanRegex = /\/|\.?\w+/
7
7
 
8
8
  attr_reader :paths
9
9
 
@@ -13,37 +13,27 @@ class Usher
13
13
  end
14
14
 
15
15
  def self.url_split(path)
16
- parts = path[0] == ?/ ? [] : [Separator::Slash]
16
+ parts = []
17
17
  ss = StringScanner.new(path)
18
18
  while !ss.eos?
19
- part = ss.scan(UrlScanRegex)
20
- case part[0]
21
- when ?.
22
- parts << Separator::Dot
23
- when ?/
24
- parts << Separator::Slash
25
- else
26
- parts << part
19
+ if part = ss.scan(UrlScanRegex)
20
+ parts << part unless part == '/'
27
21
  end
28
22
  end if path && !path.empty?
29
23
  parts
30
24
  end
31
25
 
32
26
  def self.split(path, requirements = {}, transformers = {})
33
- parts = path[0] == ?/ ? [] : [Separator::Slash]
27
+ parts = []
34
28
  ss = StringScanner.new(path)
35
29
  groups = [parts]
36
30
  current_group = parts
37
31
  while !ss.eos?
38
32
  part = ss.scan(ScanRegex)
39
33
  case part[0]
40
- when ?*, ?:
41
- type = part.slice!(0).chr.to_sym
34
+ when ?*, ?:, ?.
35
+ type = (part[1] == ?: ? part.slice!(0,2) : part.slice!(0).chr).to_sym
42
36
  current_group << Variable.new(type, part, :validator => requirements[part.to_sym], :transformer => transformers[part.to_sym])
43
- when ?.
44
- current_group << Separator::Dot
45
- when ?/
46
- current_group << Separator::Slash
47
37
  when ?(
48
38
  new_group = []
49
39
  groups << new_group
@@ -52,6 +42,7 @@ class Usher
52
42
  when ?)
53
43
  groups.pop
54
44
  current_group = groups.last
45
+ when ?/
55
46
  else
56
47
  current_group << part
57
48
  end
data/lib/usher/route.rb CHANGED
@@ -2,7 +2,6 @@ $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'route/path'
4
4
  require 'route/splitter'
5
- require 'route/separator'
6
5
  require 'route/variable'
7
6
  require 'route/http'
8
7
 
data/lib/usher.rb CHANGED
@@ -93,22 +93,21 @@ class Usher
93
93
  Array(params)
94
94
  end
95
95
 
96
- generated_path = ""
96
+ generated_path = ''
97
97
 
98
- sep_p = nil
99
98
  path.parts.each do |p|
100
99
  case p
101
100
  when Route::Variable
102
101
  case p.type
103
102
  when :*
104
- generated_path << sep_p.to_s << param_list.shift * '/'
103
+ generated_path << '/' << param_list.shift * '/'
104
+ when :'.:'
105
+ (dp = param_list.shift) && generated_path << '.' << dp.to_s
105
106
  else
106
- (dp = param_list.shift) && generated_path << sep_p.to_s << dp.to_s
107
+ (dp = param_list.shift) && generated_path << '/' << dp.to_s
107
108
  end
108
- when Route::Separator
109
- sep_p = p
110
109
  else
111
- generated_path << sep_p.to_s << p.to_s
110
+ generated_path << '/' << p.to_s
112
111
  end
113
112
  end
114
113
  unless params_hash.blank?
data/spec/path_spec.rb CHANGED
@@ -2,9 +2,6 @@ require 'lib/usher'
2
2
 
3
3
  route_set = Usher.new
4
4
 
5
- S = Usher::Route::Separator::Slash
6
- D = Usher::Route::Separator::Dot
7
-
8
5
  describe "Usher route adding" do
9
6
 
10
7
  before(:each) do
@@ -25,12 +22,12 @@ describe "Usher route adding" do
25
22
  it "should add every kind of optional route possible" do
26
23
  route_set.add_route('/a/b(/c)(/d(/e))')
27
24
  route_set.routes.first.paths.collect{|a| a.parts }.should == [
28
- [S, "a", S, "b"],
29
- [S, "a", S, "b", S, "c"],
30
- [S, "a", S, "b", S, "d"],
31
- [S, "a", S, "b", S, "d", S, "e"],
32
- [S, "a", S, "b", S, "c", S, "d"],
33
- [S, "a", S, "b", S, "c", S, "d", S, "e"]
25
+ ["a", "b"],
26
+ ["a", "b", "c"],
27
+ ["a", "b", "d"],
28
+ ["a", "b", "d", "e"],
29
+ ["a", "b", "c", "d"],
30
+ ["a", "b", "c", "d", "e"]
34
31
  ]
35
32
 
36
33
  end
data/spec/split_spec.rb CHANGED
@@ -1,36 +1,33 @@
1
1
  require 'lib/usher'
2
2
 
3
- Slash = Usher::Route::Separator::Slash
4
- Dot = Usher::Route::Separator::Dot
5
-
6
3
  describe "Usher route tokenizing" do
7
4
 
8
5
 
9
6
  it "should split / delimited routes" do
10
- Usher::Route::Splitter.new('/test/this/split').paths.first.should == [Slash, 'test', Slash, 'this', Slash, 'split']
7
+ Usher::Route::Splitter.new('/test/this/split').paths.first.should == ['test', 'this', 'split']
11
8
  end
12
9
 
13
10
  it "should group optional parts with brackets" do
14
11
  Usher::Route::Splitter.new('/test/this(/split)').paths.should == [
15
- [Slash, 'test', Slash, 'this'],
16
- [Slash, 'test', Slash, 'this', Slash, 'split']
12
+ ['test', 'this'],
13
+ ['test', 'this', 'split']
17
14
  ]
18
15
  end
19
16
 
20
17
  it "should group optional parts with brackets (for non overlapping groups)" do
21
18
  Usher::Route::Splitter.new('/test/this(/split)(/split2)').paths == [
22
- [Slash, "test", Slash, "this"],
23
- [Slash, "test", Slash, "this", Slash, "split"],
24
- [Slash, "test", Slash, "this", Slash, "split2"],
25
- [Slash, "test", Slash, "this", Slash, "split", Slash, "split2"]
19
+ ["test", "this"],
20
+ ["test", "this", "split"],
21
+ ["test", "this", "split2"],
22
+ ["test", "this", "split", "split2"]
26
23
  ]
27
24
  end
28
25
 
29
26
  it "should group nested-optional parts with brackets" do
30
27
  Usher::Route::Splitter.new('/test/this(/split(.:format))').paths == [
31
- [Slash, "test", Slash, "this"],
32
- [Slash, "test", Slash, "this", Slash, "split"],
33
- [Slash, "test", Slash, "this", Slash, "split", Dot, Usher::Route::Variable.new(:':', :format)]
28
+ ["test", "this"],
29
+ ["test", "this", "split"],
30
+ ["test", "this", "split", Usher::Route::Variable.new(:'.:', :format)]
34
31
  ]
35
32
  end
36
33
 
data/usher.gemspec CHANGED
@@ -2,20 +2,21 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{usher}
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Joshua HullJoshua Hull"]
9
9
  s.date = %q{2009-03-01}
10
10
  s.email = %q{joshbuddy@gmail.comjoshbuddy@gmail.com}
11
11
  s.extra_rdoc_files = ["History.txt", "Manifest.txt"]
12
- s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "lib/compat.rb", "lib/usher.rb", "lib/usher/exceptions.rb", "lib/usher/grapher.rb", "lib/usher/interface.rb", "lib/usher/interface/merb_interface.rb", "lib/usher/interface/rack_interface.rb", "lib/usher/interface/rack_interface/mapper.rb", "lib/usher/interface/rack_interface/route.rb", "lib/usher/interface/rails2_interface.rb", "lib/usher/interface/rails2_interface/mapper.rb", "lib/usher/node.rb", "lib/usher/node/lookup.rb", "lib/usher/route.rb", "lib/usher/route/http.rb", "lib/usher/route/path.rb", "lib/usher/route/separator.rb", "lib/usher/route/splitter.rb", "lib/usher/route/variable.rb", "rails/init.rb", "spec/generate_spec.rb", "spec/node/lookup_spec.rb", "spec/path_spec.rb", "spec/rack/dispatch_spec.rb", "spec/rails/generate_spec.rb", "spec/rails/path_spec.rb", "spec/rails/recognize_spec.rb", "spec/recognize_spec.rb", "spec/spec.opts", "spec/split_spec.rb", "usher.gemspec"]
12
+ s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "lib/compat.rb", "lib/usher.rb", "lib/usher/exceptions.rb", "lib/usher/grapher.rb", "lib/usher/interface.rb", "lib/usher/interface/merb_interface.rb", "lib/usher/interface/rack_interface.rb", "lib/usher/interface/rack_interface/mapper.rb", "lib/usher/interface/rack_interface/route.rb", "lib/usher/interface/rails2_interface.rb", "lib/usher/interface/rails2_interface/mapper.rb", "lib/usher/node.rb", "lib/usher/node/lookup.rb", "lib/usher/route.rb", "lib/usher/route/http.rb", "lib/usher/route/path.rb", "lib/usher/route/splitter.rb", "lib/usher/route/variable.rb", "rails/init.rb", "spec/generate_spec.rb", "spec/path_spec.rb", "spec/rack/dispatch_spec.rb", "spec/rails/generate_spec.rb", "spec/rails/path_spec.rb", "spec/rails/recognize_spec.rb", "spec/recognize_spec.rb", "spec/spec.opts", "spec/split_spec.rb", "usher.gemspec"]
13
13
  s.has_rdoc = true
14
14
  s.rdoc_options = ["--main", "README.rdoc"]
15
15
  s.require_paths = ["lib"]
16
16
  s.rubyforge_project = %q{usher}
17
17
  s.rubygems_version = %q{1.3.1}
18
18
  s.summary = %q{Tree-based router}
19
+ s.add_dependency(%q<fuzzy_hash>)
19
20
 
20
21
  if s.respond_to? :specification_version then
21
22
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joshbuddy-usher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua HullJoshua Hull
@@ -12,6 +12,16 @@ cert_chain: []
12
12
  date: 2009-03-01 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fuzzy_hash
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
15
25
  - !ruby/object:Gem::Dependency
16
26
  name: hoe
17
27
  type: :development
@@ -52,12 +62,10 @@ files:
52
62
  - lib/usher/route.rb
53
63
  - lib/usher/route/http.rb
54
64
  - lib/usher/route/path.rb
55
- - lib/usher/route/separator.rb
56
65
  - lib/usher/route/splitter.rb
57
66
  - lib/usher/route/variable.rb
58
67
  - rails/init.rb
59
68
  - spec/generate_spec.rb
60
- - spec/node/lookup_spec.rb
61
69
  - spec/path_spec.rb
62
70
  - spec/rack/dispatch_spec.rb
63
71
  - spec/rails/generate_spec.rb
@@ -1,21 +0,0 @@
1
- class Usher
2
- class Route
3
-
4
- class Separator
5
- private
6
- def initialize(sep)
7
- @sep = sep
8
- @sep_to_s = "#{sep}"
9
- end
10
-
11
- public
12
- def to_s
13
- @sep_to_s
14
- end
15
-
16
- Dot = Separator.new(:'.')
17
- Slash = Separator.new(:/)
18
- end
19
-
20
- end
21
- end
@@ -1,53 +0,0 @@
1
- require 'lib/usher'
2
-
3
-
4
- describe "String/regexp lookup table" do
5
-
6
- it "should accept strings and retrieve based on them" do
7
- l = Usher::Node::Lookup.new
8
- l['asd'] = 'qwe'
9
- l['asd'].should == 'qwe'
10
- end
11
-
12
- it "should accept regexs too" do
13
- l = Usher::Node::Lookup.new
14
- l[/asd.*/] = 'qwe'
15
- l['asdqweasd'].should == 'qwe'
16
- end
17
-
18
- it "should prefer string to regex matches" do
19
- l = Usher::Node::Lookup.new
20
- l['asd'] = 'qwe2'
21
- l[/asd.*/] = 'qwe'
22
- l['asd'].should == 'qwe2'
23
- end
24
-
25
- it "should allow nil keys" do
26
- l = Usher::Node::Lookup.new
27
- l[nil] = 'qwe2'
28
- l['asd'] = 'qwe'
29
- l['asd'].should == 'qwe'
30
- l[nil].should == 'qwe2'
31
- end
32
-
33
- it "should be able to delete by value for hash" do
34
- l = Usher::Node::Lookup.new
35
- l[nil] = 'qwe2'
36
- l['asd'] = 'qwe'
37
- l['asd'].should == 'qwe'
38
- l[nil].should == 'qwe2'
39
- l.delete_value('qwe2')
40
- l[nil].should == nil
41
- end
42
-
43
- it "should be able to delete by value for hash" do
44
- l = Usher::Node::Lookup.new
45
- l[/qwe.*/] = 'qwe2'
46
- l['asd'] = 'qwe'
47
- l['asd'].should == 'qwe'
48
- l['qweasd'].should == 'qwe2'
49
- l.delete_value('qwe2')
50
- l['qweasd'].should == nil
51
- end
52
-
53
- end