public
Rubygem
Fork of bmizerany/sinatra
Description: Classy web-development dressed in a DSL
Homepage: http://sinatrarb.com
Clone URL: git://github.com/adamwiggins/sinatra.git
nested resources
adamwiggins (author)
Sun Jun 08 03:56:19 -0700 2008
commit  b0b67602be141dcc835e91443e3120e5b1acbc9d
tree    945925dd6102205e84937074e377ed21753525cf
parent  9c413351c79f35a72f93cf74f3f516cab3bda4e5
...
866
867
868
869
 
870
871
872
...
994
995
996
997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
998
999
1000
1001
 
1002
1003
1004
1005
1006
 
1007
1008
1009
1010
1011
 
1012
1013
1014
...
1017
1018
1019
1020
 
1021
1022
1023
...
1026
1027
1028
1029
 
1030
1031
1032
...
866
867
868
 
869
870
871
872
...
994
995
996
 
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
 
1024
1025
1026
1027
1028
 
1029
1030
1031
1032
1033
 
1034
1035
1036
1037
...
1040
1041
1042
 
1043
1044
1045
1046
...
1049
1050
1051
 
1052
1053
1054
1055
0
@@ -866,7 +866,7 @@ module Sinatra
0
     # (Sinatra::application).
0
     FORWARD_METHODS = %w[
0
       get put post delete head template layout before error not_found
0
-      configures configure set set_options set_option enable disable use
0
+      configures configure set set_options set_option enable disable use resource
0
     ]
0
 
0
     # Create a new Application with a default configuration taken
0
@@ -994,21 +994,44 @@ module Sinatra
0
     # NOTE: The #get, #post, #put, and #delete helper methods should
0
     # be used to define events when possible.
0
     def event(method, path, options = {}, &b)
0
-      events[method].push(Event.new(path, options, &b)).last
0
+      events[method].push(Event.new(make_path(path), options, &b)).last
0
+    end
0
+
0
+    # Nest resources paths.  Example:
0
+    #
0
+    #   resource 'posts' do
0
+    #     get { 'show all posts' }
0
+    #     get ':id' { 'show post' }
0
+    #     put ':id' { 'update post' }
0
+    #   end
0
+    def resource(path, &b)
0
+      orig_path = @path_parts
0
+      @path_parts ||= []
0
+      @path_parts << path
0
+      b.call
0
+      @path_parts = orig_path
0
+    end
0
+
0
+    def make_path(path)   # :nodoc:
0
+      return path unless @path_parts
0
+
0
+      list = @path_parts
0
+      list << path if path and path != ''
0
+      ('/' + list.join('/')).squeeze('/')
0
     end
0
 
0
     # Define an event handler for GET requests.
0
-    def get(path, options={}, &b)
0
+    def get(path='', options={}, &b)
0
       event(:get, path, options, &b)
0
     end
0
 
0
     # Define an event handler for POST requests.
0
-    def post(path, options={}, &b)
0
+    def post(path='', options={}, &b)
0
       event(:post, path, options, &b)
0
     end
0
 
0
     # Define an event handler for HEAD requests.
0
-    def head(path, options={}, &b)
0
+    def head(path='', options={}, &b)
0
       event(:head, path, options, &b)
0
     end
0
 
0
@@ -1017,7 +1040,7 @@ module Sinatra
0
     # NOTE: PUT events are triggered when the HTTP request method is
0
     # PUT and also when the request method is POST and the body includes a
0
     # "_method" parameter set to "PUT".
0
-    def put(path, options={}, &b)
0
+    def put(path='', options={}, &b)
0
       event(:put, path, options, &b)
0
     end
0
 
0
@@ -1026,7 +1049,7 @@ module Sinatra
0
     # NOTE: DELETE events are triggered when the HTTP request method is
0
     # DELETE and also when the request method is POST and the body includes a
0
     # "_method" parameter set to "DELETE".
0
-    def delete(path, options={}, &b)
0
+    def delete(path='', options={}, &b)
0
       event(:delete, path, options, &b)
0
     end
0
 
...
144
145
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
148
149
...
231
232
233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
235
236
...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
0
@@ -144,6 +144,30 @@ context "Default Application Configuration" do
0
 
0
 end
0
 
0
+context "App path building" do
0
+
0
+  setup do
0
+    @app = Sinatra::Application.new
0
+  end
0
+
0
+  specify "make path with no nested resources returns the same string" do
0
+    @app.make_path('/some/path').should.equal '/some/path'
0
+  end
0
+
0
+  specify "make path with a nested resources prepends the resource" do
0
+    @app.resource('nest') do
0
+      @app.make_path('a/b').should.equal '/nest/a/b'
0
+    end
0
+  end
0
+
0
+  specify "make path with a nested resource but a nil path doesn't have a trailing slash" do
0
+    @app.resource('nest') do
0
+      @app.make_path(nil).should.equal '/nest'
0
+    end
0
+  end
0
+
0
+end
0
+
0
 context "Events in an app" do
0
   
0
   setup do
0
@@ -231,6 +255,34 @@ context "Events in an app" do
0
     body.should.equal "Look ma, a path with spaces!"
0
   end
0
   
0
+  specify "nested resource paths" do
0
+
0
+    resource 'post' do
0
+      get 'comments' do
0
+        'some comments'
0
+      end
0
+    end
0
+
0
+    get_it '/post/comments'
0
+    should.be.ok
0
+    body.should.equal 'some comments'
0
+
0
+  end
0
+
0
+  specify "omit path parameter to an event, mainly useful for nested resources" do
0
+
0
+    resource 'posts' do
0
+      get do
0
+        'all posts'
0
+      end
0
+    end
0
+
0
+    get_it '/posts'
0
+    should.be.ok
0
+    body.should.equal 'all posts'
0
+
0
+  end
0
+
0
 end
0
 
0
 

Comments