oscardelben-routes 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +19 -15
- data/lib/recognition.rb +24 -32
- data/spec/spec/routes_spec.rb +3 -2
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,30 +1,34 @@
|
|
1
1
|
= Routes
|
2
|
-
|
2
|
+
|
3
|
+
A routes library for ruby
|
3
4
|
|
4
5
|
USAGE:
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
Routes::Building.draw do
|
8
|
+
map '/posts', :controller => 'posts', :action => 'index'
|
9
|
+
map '/posts/:id', :controller => 'posts'
|
10
|
+
map '/controller/:action', {}
|
11
|
+
map '/:controller/:action/:id', {}
|
12
|
+
map '/cool', lambda { |path| "This is #{path}" }
|
13
|
+
map /.*/, :action => 'anything'
|
14
|
+
end
|
15
|
+
|
14
16
|
|
17
|
+
Routes::Recognition.new('/posts').recognize # => { :controller => 'posts', :action => 'index' }
|
15
18
|
|
16
|
-
|
19
|
+
Routes::Recognition.new('/posts/1').recognize # => { :controller => 'posts', :id => '1' }
|
17
20
|
|
18
|
-
|
21
|
+
Routes::Recognition.new('/controller/new').recognize # => { :action => 'new' }
|
19
22
|
|
20
|
-
|
23
|
+
Routes::Recognition.new('/posts/show/1').recognize # => { :controller => 'posts', :action => 'show', :id => '1' }
|
21
24
|
|
22
|
-
|
25
|
+
Routes::Recognition.new('/something').recognize # => { :action => 'anything' }
|
23
26
|
|
24
|
-
|
27
|
+
Routes::Recognition.new('/cool').recognize.call('/cool') # => "This is /cool"
|
25
28
|
|
26
|
-
Routes::Recognition.new('/cool').recognize # => "This is /cool"
|
27
29
|
|
30
|
+
Note that the library will not execute lambdas itself, so it's your responsibility to test the result returned. This allow more flexibility in my opinion because you can pass additional parameters to it like the env object.
|
28
31
|
|
29
32
|
CONTRIBUTIONS
|
33
|
+
|
30
34
|
I'm open to contributions and suggestions. Drop me an email at info@oscardelben.com
|
data/lib/recognition.rb
CHANGED
@@ -15,39 +15,15 @@ module Routes
|
|
15
15
|
|
16
16
|
filter_route!
|
17
17
|
|
18
|
-
return
|
18
|
+
return result if result
|
19
19
|
end
|
20
20
|
|
21
21
|
# Nothing matched
|
22
|
-
|
22
|
+
result
|
23
23
|
end
|
24
24
|
|
25
25
|
private
|
26
|
-
|
27
|
-
def segments_from_string(path)
|
28
|
-
path[1..-1].split('/')
|
29
|
-
end
|
30
|
-
|
31
|
-
def give_back_result
|
32
|
-
if result.respond_to?(:call)
|
33
|
-
result.call(path)
|
34
|
-
else
|
35
|
-
result
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def update_result(key, value)
|
40
|
-
@result.merge!(key => value)
|
41
|
-
end
|
42
|
-
|
43
|
-
def route_segments
|
44
|
-
segments_from_string(route_pattern)
|
45
|
-
end
|
46
|
-
|
47
|
-
def path_segments
|
48
|
-
segments_from_string(path)
|
49
|
-
end
|
50
|
-
|
26
|
+
|
51
27
|
def filter_route!
|
52
28
|
if route_pattern.is_a? String
|
53
29
|
match_with_string
|
@@ -55,23 +31,39 @@ module Routes
|
|
55
31
|
match_with_regexp
|
56
32
|
end
|
57
33
|
end
|
58
|
-
|
34
|
+
|
59
35
|
def match_with_string
|
60
36
|
if compatible_segments?
|
61
37
|
match_segments
|
62
38
|
else
|
63
|
-
|
39
|
+
self.result = nil
|
64
40
|
end
|
65
41
|
end
|
66
42
|
|
67
43
|
def match_with_regexp
|
68
44
|
if route_pattern =~ path
|
69
|
-
return
|
45
|
+
return result
|
70
46
|
else
|
71
|
-
result = nil
|
47
|
+
self.result = nil
|
72
48
|
end
|
73
49
|
end
|
74
50
|
|
51
|
+
def segments_from_string(path)
|
52
|
+
path[1..-1].split('/')
|
53
|
+
end
|
54
|
+
|
55
|
+
def update_result(key, value)
|
56
|
+
self.result.merge!(key => value)
|
57
|
+
end
|
58
|
+
|
59
|
+
def route_segments
|
60
|
+
segments_from_string(route_pattern)
|
61
|
+
end
|
62
|
+
|
63
|
+
def path_segments
|
64
|
+
segments_from_string(path)
|
65
|
+
end
|
66
|
+
|
75
67
|
# TODO: path_segments is calculated 2 times
|
76
68
|
def compatible_segments?
|
77
69
|
route_segments.size == path_segments.size
|
@@ -84,7 +76,7 @@ module Routes
|
|
84
76
|
if route_segment[0] == ?:
|
85
77
|
update_result(eval(route_segment), path_segment)
|
86
78
|
elsif path_segment != route_segment
|
87
|
-
|
79
|
+
self.result = nil
|
88
80
|
break
|
89
81
|
end
|
90
82
|
end
|
data/spec/spec/routes_spec.rb
CHANGED
@@ -83,8 +83,9 @@ describe Routes do
|
|
83
83
|
Routes::Recognition.new('/something').recognize.should == { :action => 'anything' }
|
84
84
|
end
|
85
85
|
|
86
|
-
it "should
|
87
|
-
Routes::Recognition.new('/cool').recognize
|
86
|
+
it "should return lambda routes" do
|
87
|
+
route = Routes::Recognition.new('/cool').recognize
|
88
|
+
route.call('/cool').should == "This is /cool"
|
88
89
|
end
|
89
90
|
|
90
91
|
end
|