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

public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Added docuemntation for the Router and the Router::Behavior and 
Router::Proxy.
mtodd (author)
Fri Oct 10 22:52:56 -0700 2008
commit  457c28237b6f870a815b228f1d7339c76b05e5da
tree    bab20e82f8d221060a9f692c35e3790b29b5ac08
parent  19a3402711af8eaa1a4a706b951308e24ea8d1e9
...
6
7
8
9
 
10
11
12
 
13
14
 
15
16
17
18
19
20
21
 
22
23
24
...
26
27
28
29
 
30
31
32
33
34
35
36
 
37
38
39
...
41
42
43
44
 
45
46
47
48
49
50
51
52
53
 
54
55
56
 
 
57
58
59
...
63
64
65
 
 
66
67
68
69
 
70
 
 
71
72
73
74
75
76
 
 
77
78
79
...
84
85
86
87
 
88
89
90
 
91
92
93
94
95
96
97
98
 
 
 
 
 
99
100
101
...
105
106
107
108
109
 
 
 
 
 
 
 
 
 
 
 
 
 
110
111
112
113
 
114
115
116
117
 
118
119
120
121
 
122
123
124
125
126
 
127
128
129
 
130
131
132
133
134
 
 
 
 
 
135
136
137
138
 
 
139
140
141
142
143
144
 
145
146
147
...
152
153
154
155
 
156
157
158
159
160
161
 
162
163
164
165
166
167
 
 
168
169
170
171
172
173
 
174
175
176
...
179
180
181
182
 
183
184
 
185
186
187
188
 
189
190
191
192
193
194
 
 
 
 
 
 
195
196
197
...
199
200
201
202
203
 
 
 
 
 
204
205
206
207
 
208
209
210
211
212
213
 
214
215
216
...
222
223
224
225
 
226
227
228
...
6
7
8
 
9
10
11
 
12
13
 
14
15
16
17
18
19
20
 
21
22
23
24
...
26
27
28
 
29
30
31
32
33
34
35
 
36
37
38
39
...
41
42
43
 
44
45
46
47
48
 
49
50
51
 
52
53
54
55
56
57
58
59
60
...
64
65
66
67
68
69
70
71
 
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
...
91
92
93
 
94
95
96
 
97
98
 
 
 
 
 
 
 
99
100
101
102
103
104
105
106
...
110
111
112
 
 
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 
129
130
131
132
 
133
134
135
136
 
137
138
139
140
141
 
142
143
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
...
173
174
175
 
176
177
178
179
180
181
 
182
183
184
185
186
 
 
187
188
189
190
191
192
193
 
194
195
196
197
...
200
201
202
 
203
204
 
205
206
207
208
 
209
210
211
 
 
 
 
212
213
214
215
216
217
218
219
220
...
222
223
224
 
 
225
226
227
228
229
230
231
232
 
233
234
235
236
237
238
 
239
240
241
242
...
248
249
250
 
251
252
253
254
0
@@ -6,19 +6,19 @@ require 'merb-core/dispatch/router/route'
0
 module Merb
0
   # Router stores route definitions and finds the first
0
   # route that matches the incoming request URL.
0
- #
0
+ #
0
   # Then information from route is used by dispatcher to
0
   # call action on the controller.
0
- #
0
+ #
0
   # ==== Routes compilation.
0
- #
0
+ #
0
   # The most interesting method of Router (and heart of
0
   # route matching machinery) is match method generated
0
   # on the fly from routes definitions. It is called routes
0
   # compilation. Generated match method body contains
0
   # one if/elsif statement that picks the first matching route
0
   # definition and sets values to named parameters of the route.
0
- #
0
+ #
0
   # Compilation is synchronized by mutex.
0
   class Router
0
     @routes = []
0
@@ -26,14 +26,14 @@ module Merb
0
     @resource_routes = {}
0
     @compiler_mutex = Mutex.new
0
     @root_behavior = Behavior.new.defaults(:action => "index")
0
-
0
+
0
     # Raised when route lookup fails.
0
     class RouteNotFound < StandardError; end;
0
     # Raised when parameters given to generation
0
     # method do not match route parameters.
0
     class GenerationError < StandardError; end;
0
     class NotCompiledError < StandardError; end;
0
-
0
+
0
     class << self
0
       # @private
0
       attr_accessor :routes, :named_routes, :resource_routes, :root_behavior
0
@@ -41,19 +41,20 @@ module Merb
0
       # Creates a route building context and evaluates the block in it. A
0
       # copy of +root_behavior+ (and instance of Behavior) is copied as
0
       # the context.
0
- #
0
+ #
0
       # ==== Parameters
0
       # first<Array>::
0
       # An array containing routes that should be prepended to the routes
0
       # defined in the block.
0
- #
0
       # last<Array>::
0
       # An array containing routes that should be appended to the routes
0
       # defined in the block.
0
- #
0
+ #
0
       # ==== Returns
0
       # Merb::Router::
0
       # Returns self to allow chaining of methods.
0
+ #
0
+ # @api public
0
       def prepare(first = [], last = [], &block)
0
         @routes = []
0
         root_behavior._with_proxy(&block)
0
@@ -63,17 +64,23 @@ module Merb
0
       end
0
       
0
       # Appends route in the block to routing table.
0
+ #
0
+ # @api public
0
       def append(&block)
0
         prepare(routes, [], &block)
0
       end
0
-
0
+
0
       # Prepends routes in the block to routing table.
0
+ #
0
+ # @api public
0
       def prepend(&block)
0
         prepare([], routes, &block)
0
       end
0
       
0
       # Clears the routing table. Route generation and request matching
0
       # won't work anymore until a new routing table is built.
0
+ #
0
+ # @api private
0
       def reset!
0
         class << self
0
           alias_method :match, :match_before_compilation
0
@@ -84,18 +91,16 @@ module Merb
0
       # Finds route matching URI of the request and returns a tuple of
0
       # [route index, route params]. This method is called by the
0
       # dispatcher and isn't as useful in applications.
0
- #
0
+ #
0
       # ==== Parameters
0
       # request<Merb::Request>:: request to match.
0
- #
0
+ #
0
       # ==== Returns
0
- # <Array(Integer, Hash)::
0
- # Two-tuple: route index and route parameters. Route
0
- # parameters are :controller, :action and all the named
0
- # segments of the route.
0
- #
0
- # ---
0
- # @private
0
+ # Array[Integer, Hash]::
0
+ # Two-tuple: route index and route parameters. Route parameters
0
+ # are :controller, :action and all the named segments of the route.
0
+ #
0
+ # @api private
0
       def route_for(request) #:nodoc:
0
         index, params = match(request)
0
         route = routes[index] if index
0
@@ -105,43 +110,59 @@ module Merb
0
         end
0
         [route, params]
0
       end
0
-
0
- # Just a placeholder for the compiled match method
0
+
0
+ # A placeholder for the compiled match method.
0
+ #
0
+ # ==== Notes
0
+ # This method is aliased as +match+ but this method gets overridden with
0
+ # the actual +match+ method (generated from the routes definitions) after
0
+ # being compiled. This method is only ever called before routes are
0
+ # compiled.
0
+ #
0
+ # ==== Raises
0
+ # NotCompiledError:: routes have not been compiled yet.
0
+ #
0
+ # @api private
0
       def match_before_compilation(request) #:nodoc:
0
         raise NotCompiledError, "The routes have not been compiled yet"
0
       end
0
-
0
+
0
       alias_method :match, :match_before_compilation
0
       
0
       # Generates a URL from the params
0
- #
0
+ #
0
       # ==== Parameters
0
       # name<Symbol>::
0
       # The name of the route to generate
0
- #
0
+ #
0
       # anonymous_params<Object>::
0
       # An array of anonymous parameters to generate the route
0
       # with. These parameters are assigned to the route parameters
0
       # in the order that they are passed.
0
- #
0
+ #
0
       # params<Hash>::
0
       # Named parameters to generate the route with.
0
- #
0
+ #
0
       # defaults<Hash>::
0
       # A hash of default parameters to generate the route with.
0
       # This is usually the request parameters. If there are any
0
       # required params that are missing to generate the route,
0
       # they are pulled from this hash.
0
+ #
0
+ # ==== Example
0
+ # url(:edit_node, node.id, :foo => "bar")
0
+ # url(:edit_site_rating, site.id, rating.id, :foo => "bar")
0
+ #
0
       # ==== Returns
0
       # String:: The generated URL
0
- # ---
0
- # @private
0
+ #
0
+ # @api private
0
       def url(name, *args)
0
         unless name.is_a?(Symbol)
0
           args.unshift(name)
0
           name = :default
0
         end
0
-
0
+
0
         unless route = Merb::Router.named_routes[name]
0
           raise Merb::Router::GenerationError, "Named route not found: #{name}"
0
         end
0
@@ -152,25 +173,25 @@ module Merb
0
       end
0
       
0
       # Generates a URL from the resource(s)
0
- #
0
+ #
0
       # ==== Parameters
0
       # resources<Symbol,Object>::
0
       # The identifiers for the resource route to generate. These
0
       # can either be symbols or objects. Symbols denote resource
0
       # collection routes and objects denote the members.
0
- #
0
+ #
0
       # params<Hash>::
0
       # Any extra parameters needed to generate the route.
0
       # ==== Returns
0
       # String:: The generated URL
0
- # ---
0
- # @private
0
+ #
0
+ # @api private
0
       def resource(*args)
0
         defaults = args.pop
0
         options = extract_options_from_args!(args) || {}
0
         key = []
0
         params = []
0
-
0
+
0
         args.each do |arg|
0
           if arg.is_a?(Symbol) || arg.is_a?(String)
0
             key << arg.to_s
0
@@ -179,19 +200,21 @@ module Merb
0
             params << arg
0
           end
0
         end
0
-
0
+
0
         params << options
0
-
0
+
0
         unless route = Merb::Router.resource_routes[key]
0
           raise Merb::Router::GenerationError, "Resource route not found: #{args.inspect}"
0
         end
0
-
0
+
0
         route.generate(params, defaults)
0
       end
0
-
0
- private
0
-
0
- # Defines method with a switch statement that does routes recognition.
0
+
0
+ private
0
+
0
+ # Compiles the routes and creates the +match+ method.
0
+ #
0
+ # @api private
0
       def compile
0
         if routes.any?
0
           eval(compiled_statement, binding, "Generated Code for Router", 1)
0
@@ -199,18 +222,21 @@ module Merb
0
           reset!
0
         end
0
       end
0
-
0
- # Generates method that does route recognition with a switch statement.
0
+
0
+ # Generates the method for evaluation defining a +match+ method to match
0
+ # a request with the defined routes.
0
+ #
0
+ # @api private
0
       def compiled_statement
0
         @compiler_mutex.synchronize do
0
           condition_keys, if_statements = Set.new, ""
0
-
0
+
0
           routes.each_with_index do |route, i|
0
             route.freeze
0
             route.conditions.keys.each { |key| condition_keys << key }
0
             if_statements << route.compiled_statement(i == 0)
0
           end
0
-
0
+
0
           statement = "def match(request)\n"
0
           statement << condition_keys.inject("") do |cached, key|
0
             cached << " cached_#{key} = request.#{key}.to_s\n"
0
@@ -222,7 +248,7 @@ module Merb
0
           statement << "end"
0
         end
0
       end
0
-
0
+
0
     end # class << self
0
   end
0
 end
...
3
4
5
6
7
 
 
8
9
10
11
12
13
14
 
 
 
 
15
16
17
18
 
19
20
21
22
 
 
 
 
 
 
23
24
25
26
 
 
 
 
 
 
27
28
29
30
 
 
 
 
 
 
31
32
33
...
35
36
37
38
 
39
40
41
...
49
50
51
52
 
53
54
55
 
 
 
 
 
 
56
57
58
59
60
 
 
 
 
 
 
61
62
63
64
65
66
67
 
68
69
70
 
 
 
 
 
 
 
71
72
73
...
78
79
80
81
 
82
83
84
85
86
 
87
88
 
89
90
91
...
100
101
102
103
 
104
105
106
107
 
 
108
109
110
...
113
114
115
116
 
117
118
119
 
120
121
122
123
 
124
125
126
127
128
 
129
130
 
131
132
133
 
134
135
136
137
138
 
139
140
141
142
143
 
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
 
174
175
176
177
 
178
179
180
 
181
182
183
184
 
185
186
187
188
 
189
190
 
191
192
193
 
194
195
196
197
198
 
 
199
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
232
233
234
235
236
 
 
237
238
239
 
240
241
242
...
247
248
249
250
 
 
251
252
253
...
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
...
289
290
291
292
 
293
294
295
296
297
298
299
 
300
301
302
303
 
304
305
306
 
307
308
309
310
311
312
 
 
313
314
315
 
316
317
318
319
 
320
321
322
...
325
326
327
328
 
329
330
331
332
 
333
334
335
 
 
336
337
338
339
 
 
 
 
340
341
342
 
343
344
345
346
347
348
 
349
350
351
352
353
354
 
 
355
356
357
358
 
359
360
 
361
362
363
364
365
 
366
367
368
...
372
373
374
375
 
376
377
 
378
379
380
381
382
 
383
384
385
386
 
387
388
389
390
 
 
391
392
393
...
402
403
404
405
 
406
407
408
409
 
410
411
412
413
 
414
415
416
 
417
418
 
419
420
421
 
422
423
424
...
426
427
428
429
430
 
 
431
432
433
434
435
436
437
 
438
439
440
441
442
443
 
444
445
446
 
447
448
449
450
451
452
453
 
 
454
455
456
...
458
459
460
 
 
 
 
 
 
 
 
 
 
461
462
463
464
465
466
467
468
 
 
469
470
471
 
 
472
473
 
 
474
475
476
477
478
 
479
480
481
 
482
483
484
...
487
488
489
 
 
490
491
492
...
496
497
498
 
 
499
500
 
 
501
502
503
504
505
506
507
 
 
 
508
509
510
511
 
 
512
513
514
515
516
 
 
 
 
 
 
 
 
517
518
519
 
520
521
522
...
529
530
531
 
 
532
533
534
...
545
546
547
548
549
550
551
 
 
 
 
 
 
 
552
553
554
555
556
557
558
559
 
 
 
 
 
 
 
 
560
561
562
563
564
 
 
 
 
 
 
 
565
566
 
567
568
 
569
570
571
...
594
595
596
597
 
 
 
 
 
 
 
598
599
600
601
602
 
 
 
 
 
 
 
603
604
605
...
607
608
609
610
 
 
 
 
 
 
 
 
 
 
 
 
611
612
613
...
616
617
618
619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
620
621
622
623
 
624
625
626
627
...
3
4
5
 
 
6
7
8
9
10
11
 
 
 
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
...
55
56
57
 
58
59
60
61
...
69
70
71
 
72
73
 
 
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
 
 
95
96
 
 
97
98
99
100
101
102
103
104
105
106
...
111
112
113
 
114
115
116
117
118
 
119
120
 
121
122
123
124
...
133
134
135
 
136
137
138
 
 
139
140
141
142
143
...
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
174
175
 
176
177
178
179
180
181
182
 
183
184
185
186
 
187
188
189
 
190
191
192
193
 
194
195
 
 
196
197
198
 
199
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
232
233
 
 
234
235
236
237
238
 
239
240
241
242
243
244
245
 
246
247
248
 
249
250
251
252
 
253
254
255
 
256
257
258
 
259
260
261
 
262
263
264
265
266
 
 
267
268
269
270
 
271
272
273
274
...
279
280
281
 
282
283
284
285
286
...
288
289
290
 
291
292
293
 
294
295
296
297
298
 
 
299
300
301
302
303
 
304
305
306
307
 
308
309
310
311
 
312
313
314
 
 
315
316
317
318
319
...
322
323
324
 
325
326
327
328
329
330
331
 
332
333
334
335
 
336
337
338
 
339
340
341
342
343
 
 
344
345
346
347
 
348
349
350
351
 
352
353
354
355
...
358
359
360
 
361
362
363
364
 
365
366
 
 
367
368
369
370
371
 
372
373
374
375
376
377
 
378
379
380
381
382
383
 
384
385
386
387
388
 
 
389
390
391
392
393
 
394
395
 
396
397
398
399
400
 
401
402
403
404
...
408
409
410
 
411
412
 
413
414
415
416
417
 
418
419
420
421
 
422
423
424
 
 
425
426
427
428
429
...
438
439
440
 
441
442
443
444
 
445
446
447
448
 
449
450
451
 
452
453
 
454
455
456
 
457
458
459
460
...
462
463
464
 
 
465
466
467
468
469
470
471
472
 
473
474
475
476
477
478
 
479
480
481
 
482
483
484
485
486
487
 
 
488
489
490
491
492
...
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
 
 
513
514
515
 
 
516
517
518
519
520
521
522
523
524
525
 
526
527
528
 
529
530
531
532
...
535
536
537
538
539
540
541
542
...
546
547
548
549
550
551
552
553
554
555
556
557
558
 
 
 
559
560
561
562
 
 
 
563
564
565
 
 
 
 
566
567
568
569
570
571
572
573
574
575
 
576
577
578
579
...
586
587
588
589
590
591
592
593
...
604
605
606
 
 
 
 
607
608
609
610
611
612
613
614
615
616
617
618
619
 
 
620
621
622
623
624
625
626
627
628
629
630
631
 
632
633
634
635
636
637
638
639
 
640
641
 
642
643
644
645
...
668
669
670
 
671
672
673
674
675
676
677
678
679
 
 
 
680
681
682
683
684
685
686
687
688
689
...
691
692
693
 
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
...
711
712
713
 
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
 
732
733
734
735
736
0
@@ -3,31 +3,51 @@ module Merb
0
   class Router
0
     
0
     class Behavior
0
-
0
- class Error < StandardError; end;
0
+
0
+ class Error < StandardError; end
0
       
0
       # Proxy catches any methods and proxies them to the current behavior.
0
       # This allows building routes without constantly having to catching the
0
       # yielded behavior object
0
- # ---
0
- # @private
0
- class Proxy #:nodoc:
0
+ #
0
+ # @api private
0
+ class Proxy
0
+
0
         # Undefine as many methods as possible so that everything can be proxied
0
         # along to the behavior
0
         instance_methods.each { |m| undef_method m unless %w[ __id__ __send__ class kind_of? respond_to? assert_kind_of should should_not instance_variable_set instance_variable_get instance_eval].include?(m) }
0
         
0
+ # @api private
0
         def initialize
0
           @behaviors = []
0
         end
0
         
0
+ # Puts a behavior on the bottom of the stack.
0
+ #
0
+ # ==== Notes
0
+ # The behaviors keep track of nested scopes.
0
+ #
0
+ # @api private
0
         def push(behavior)
0
           @behaviors.push(behavior)
0
         end
0
         
0
+ # Removes the top-most behavior.
0
+ #
0
+ # ==== Notes
0
+ # This occurs at the end of a nested scope (namespace, etc).
0
+ #
0
+ # @api private
0
         def pop
0
           @behaviors.pop
0
         end
0
         
0
+ # Tests whether the top-most behavior responds to the arguments.
0
+ #
0
+ # ==== Notes
0
+ # Behaviors contain the actual functionality of the proxy.
0
+ #
0
+ # @api private
0
         def respond_to?(*args)
0
           super || @behaviors.last.respond_to?(*args)
0
         end
0
@@ -35,7 +55,7 @@ module Merb
0
         # Rake does some stuff with methods in the global namespace, so if I don't
0
         # explicitly define the Behavior methods to proxy her