GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Resource-oriented open source Ruby framework for Web apps.
Homepage: http://rubywaves.com/
Clone URL: git://github.com/dyoder/waves.git
Early implementation of named mapping.
dyoder (author)
Wed May 21 19:15:41 -0700 2008
commit  d6c54778c7aa86716b3bd8ac8c982bd792bf5bb8
tree    e4bb450fc947e42c45b015cc9cb31ddc8b74ad51
parent  732321ea1924ee0337bd3d7fd13abafc9eb46968
...
5
6
7
8
 
9
10
11
 
12
13
14
...
19
20
21
22
 
23
...
5
6
7
 
8
9
 
 
10
11
12
13
...
18
19
20
 
21
22
0
@@ -5,10 +5,9 @@ module Waves
0
       module Filebase
0
         
0
         def self.included(app)
0
- app.module_eval
0
+ app.module_eval do
0
             auto_eval( :Models ) do
0
- include AutoCode
0
- auto_eval true { include Filebase::Model[ :db / self.name.snake_case ] }
0
+ auto_eval( true ) { include Filebase::Model[ :db / self.name.snake_case ] }
0
             end
0
           end
0
         end
0
@@ -19,4 +18,4 @@ module Waves
0
     
0
   end
0
   
0
-end
0
+end
0
\ No newline at end of file
...
139
140
141
142
143
144
145
 
 
 
 
 
 
146
147
148
...
198
199
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
202
203
...
262
263
264
 
265
266
...
139
140
141
 
 
 
 
142
143
144
145
146
147
148
149
150
...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
...
290
291
292
293
294
295
0
@@ -139,10 +139,12 @@ module Waves
0
     # Maps a request to a block. Don't use this method directly unless you know what
0
     # you're doing. Use +path+ or +url+ instead.
0
     def map( path, options = {}, params = {}, &block )
0
- if path.is_a? Hash
0
- params = options
0
- options = path
0
- else
0
+ case path
0
+ when Array
0
+ options[:path] = Regexp.new( "^#{options[:generator].call( *path ).gsub('/','\/')}$" )
0
+ when Hash
0
+ params = options; options = path
0
+ when String
0
         options[:path] = path
0
       end
0
       mapping << [ options, params, block ]
0
@@ -198,6 +200,32 @@ module Waves
0
       end
0
       return false
0
     end
0
+
0
+ #
0
+ # This method is used to create named routes, like this (resource and name would likely be regular expressions):
0
+ # path [ resource, name ], :method => :get,
0
+ # :generator => generator( :show ) { |resource,name| "/#{resource}/#{name}" } do | r,n |
0
+ # ...
0
+ # end
0
+ #
0
+ def generator( name, &block )
0
+ @named ||= {}
0
+ @named[name] = block
0
+ end
0
+
0
+ class NamedProxy
0
+ def initialize( named ) @named = named ; end
0
+ def method_missing(name,*args) @named[name].call(*args) ; end
0
+ end
0
+
0
+ # You can access named routes using #named, like this:
0
+ #
0
+ # Waves.mappings.named.show( 'post', 'named-mappings' )
0
+ #
0
+ def named
0
+ @named_proxy ||= NamedProxy.new( @named ||= {} )
0
+ end
0
+
0
 
0
     # Match the given request against the defined rules. This is typically only called
0
     # by a dispatcher object, so you shouldn't typically use it directly.
0
@@ -262,5 +290,6 @@ module Waves
0
       end
0
     end
0
   end
0
+
0
 
0
 end

Comments

    No one has commented yet.