named-routes 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/named-routes/routes.rb +5 -1
- data/spec/named-routes/routes_spec.rb +40 -33
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.4
|
data/lib/named-routes/routes.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module NamedRoutes
|
2
2
|
class Routes
|
3
3
|
class_inheritable_accessor :host, :prefix
|
4
|
-
|
4
|
+
|
5
5
|
extend(Module.new do
|
6
6
|
def instance
|
7
7
|
@instance ||= new
|
@@ -94,6 +94,10 @@ module NamedRoutes
|
|
94
94
|
end
|
95
95
|
end)
|
96
96
|
|
97
|
+
def eval(*args)
|
98
|
+
self.class.eval(*args)
|
99
|
+
end
|
100
|
+
|
97
101
|
def as_json(*args)
|
98
102
|
self.class.defined_routes
|
99
103
|
end
|
@@ -6,8 +6,8 @@ module NamedRoutes
|
|
6
6
|
NamedRoutes.routes.host = "example.com"
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
@
|
9
|
+
def routes_class
|
10
|
+
@routes_class ||= begin
|
11
11
|
routes_class = Class.new(NamedRoutes::Routes)
|
12
12
|
routes_class.route(:root, "/")
|
13
13
|
routes_class.route(:current_user_category_top_choices, "/current-user/:category/top-choices")
|
@@ -19,27 +19,27 @@ module NamedRoutes
|
|
19
19
|
describe "uri definition" do
|
20
20
|
context "when params hash is not given" do
|
21
21
|
it "returns the definition" do
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
routes_class.root.should == "/"
|
23
|
+
routes_class.current_user_category_top_choices.should == "/current-user/:category/top-choices"
|
24
|
+
routes_class.decision_stream.should == "/decision-streams/:stream_id"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
context "when params hash is given" do
|
29
29
|
it "returns the uri with the param replaced with the given param value with additional params added as url params" do
|
30
|
-
schemed_uri_1 =
|
30
|
+
schemed_uri_1 = routes_class.current_user_category_top_choices(:category => "cars", :foo => "bar", :baz => {"one" => "two three"})
|
31
31
|
|
32
32
|
path, query = schemed_uri_1.split("?")
|
33
33
|
path.should == "/current-user/cars/top-choices"
|
34
34
|
query.should include("foo=bar")
|
35
35
|
query.should include("baz[one]=two+three")
|
36
|
-
|
36
|
+
routes_class.decision_stream(:stream_id => 99).should == "/decision-streams/99"
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
context "when a prefix is given" do
|
41
|
-
def
|
42
|
-
@
|
41
|
+
def routes_class
|
42
|
+
@routes_class ||= begin
|
43
43
|
routes_class = Class.new(NamedRoutes::Routes)
|
44
44
|
routes_class.prefix = "general"
|
45
45
|
routes_class.route(:root, "/")
|
@@ -51,18 +51,18 @@ module NamedRoutes
|
|
51
51
|
|
52
52
|
context "when default and include_prefix argument is true" do
|
53
53
|
it "appends the prefix to the returned uris" do
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
routes_class.root.should == "/general/"
|
55
|
+
routes_class.current_user_category_top_choices.should == "/general/current-user/:category/top-choices"
|
56
|
+
routes_class.decision_stream.should == "/general/decision-streams/:stream_id"
|
57
|
+
routes_class.current_user_category_top_choices(:category => "cars").should == "/general/current-user/cars/top-choices"
|
58
|
+
routes_class.decision_stream(:stream_id => 99).should == "/general/decision-streams/99"
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
context "when include_prefix argument is false in the uri definition" do
|
63
63
|
it "does not append the prefix to the returned uris" do
|
64
|
-
|
65
|
-
|
64
|
+
routes_class.uri(:raw_path, "/raw/path", false).should == "/raw/path"
|
65
|
+
routes_class.raw_path.should == "/raw/path"
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
@@ -71,38 +71,45 @@ module NamedRoutes
|
|
71
71
|
describe ".eval" do
|
72
72
|
context "when params hash is not given" do
|
73
73
|
it "returns the definition" do
|
74
|
-
|
74
|
+
routes_class.eval("/current-user/:category/top-choices").should == "/current-user/:category/top-choices"
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
78
|
context "when params hash is given" do
|
79
79
|
it "returns the uri with the param replaced with the given param value with additional params added as url params" do
|
80
|
-
schemed_uri_1 =
|
80
|
+
schemed_uri_1 = routes_class.current_user_category_top_choices(:category => "cars", :foo => "bar", :baz => {"one" => "two three"})
|
81
81
|
|
82
82
|
path, query = schemed_uri_1.split("?")
|
83
83
|
path.should == "/current-user/cars/top-choices"
|
84
84
|
query.should include("foo=bar")
|
85
85
|
query.should include("baz[one]=two+three")
|
86
|
-
|
86
|
+
routes_class.eval("/decision-streams/:stream_id", :stream_id => 99).should == "/decision-streams/99"
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
+
describe "#eval" do
|
92
|
+
it "delegates to self.class.eval" do
|
93
|
+
routes = routes_class.new
|
94
|
+
routes.eval("/current-user/:category/top-choices").should == routes_class.eval("/current-user/:category/top-choices")
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
91
98
|
describe ".http" do
|
92
99
|
it "returns a full http schemed_uri (with ::NamedRoutes.host) for the given named route" do
|
93
|
-
|
100
|
+
routes_class.http.decision_stream(:stream_id => "11").should == "http://example.com/decision-streams/11"
|
94
101
|
end
|
95
102
|
end
|
96
103
|
|
97
104
|
describe ".https" do
|
98
105
|
it "returns a full https schemed_uri (with ::NamedRoutes.host) for the given named route" do
|
99
|
-
|
106
|
+
routes_class.https.decision_stream(:stream_id => "11").should == "https://example.com/decision-streams/11"
|
100
107
|
end
|
101
108
|
end
|
102
109
|
|
103
110
|
describe "#normalize" do
|
104
|
-
def
|
105
|
-
@
|
111
|
+
def routes_class
|
112
|
+
@routes_class ||= begin
|
106
113
|
route_class = Class.new(NamedRoutes::Routes)
|
107
114
|
route_class
|
108
115
|
end
|
@@ -110,34 +117,34 @@ module NamedRoutes
|
|
110
117
|
|
111
118
|
context "when there is no prefix" do
|
112
119
|
before do
|
113
|
-
|
120
|
+
routes_class.prefix.should == nil
|
114
121
|
end
|
115
122
|
|
116
123
|
it "returns the given uri" do
|
117
|
-
|
124
|
+
routes_class.normalize("/prefix/foo/bar").should == "/prefix/foo/bar"
|
118
125
|
end
|
119
126
|
end
|
120
127
|
|
121
128
|
context "when there is a prefix" do
|
122
129
|
context "when the prefix begins with a /" do
|
123
130
|
before do
|
124
|
-
|
131
|
+
routes_class.prefix = "/prefix"
|
125
132
|
end
|
126
133
|
|
127
134
|
it "strips out the prefix from the beginning" do
|
128
|
-
|
129
|
-
|
135
|
+
routes_class.normalize("/prefix/foo/bar").should == "/foo/bar"
|
136
|
+
routes_class.normalize("/prefix/foo/prefix/bar").should == "/foo/prefix/bar"
|
130
137
|
end
|
131
138
|
end
|
132
139
|
|
133
140
|
context "when the prefix does not begin with a /" do
|
134
141
|
before do
|
135
|
-
|
142
|
+
routes_class.prefix = "prefix"
|
136
143
|
end
|
137
144
|
|
138
145
|
it "strips out the prefix from the beginning" do
|
139
|
-
|
140
|
-
|
146
|
+
routes_class.normalize("/prefix/foo/bar").should == "/foo/bar"
|
147
|
+
routes_class.normalize("/prefix/foo/prefix/bar").should == "/foo/prefix/bar"
|
141
148
|
end
|
142
149
|
end
|
143
150
|
end
|
@@ -145,7 +152,7 @@ module NamedRoutes
|
|
145
152
|
|
146
153
|
describe ".as_json" do
|
147
154
|
it "returns a hash of all of the route methods as keys and the definions as values for the instance" do
|
148
|
-
|
155
|
+
routes_class.as_json.should == {
|
149
156
|
"root" => "/",
|
150
157
|
"current_user_category_top_choices" => "/current-user/:category/top-choices",
|
151
158
|
"decision_stream" => "/decision-streams/:stream_id"
|
@@ -155,7 +162,7 @@ module NamedRoutes
|
|
155
162
|
|
156
163
|
describe "#as_json" do
|
157
164
|
it "returns a hash of all of the route methods as keys and the definions as values" do
|
158
|
-
|
165
|
+
routes_class.instance.as_json.should == {
|
159
166
|
"root" => "/",
|
160
167
|
"current_user_category_top_choices" => "/current-user/:category/top-choices",
|
161
168
|
"decision_stream" => "/decision-streams/:stream_id"
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 4
|
9
|
+
version: 0.2.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Brian Takita
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-19 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|