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
merb-core / lib / merb-core / dispatch / router / behavior.rb
100644 814 lines (743 sloc) 29.478 kb
1
2
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
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
320
321
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
356
357
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
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
457
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
485
486
487
488
489
490
491
492
493
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
533
534
535
536
537
538
539
540
541
542
543
544
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
module Merb
 
  class Router
 
    # The Behavior class is an interim route-building class that ties
    # pattern-matching +conditions+ to output parameters, +params+.
    #---
    # @public
    class Behavior
      attr_reader :placeholders, :conditions, :params
      attr_accessor :parent
      @@parent_resource = []
      class << self
 
        # ==== Parameters
        # string<String>:: The string in which to count parentheses.
        # pos<Fixnum>:: The last character for counting.
        #
        # ==== Returns
        # Fixnum::
        # The number of open parentheses in string, up to and including pos.
        def count_parens_up_to(string, pos)
          string[0..pos].gsub(/[^\(]/, '').size
        end
 
        # ==== Parameters
        # string1<String>:: The string to concatenate with.
        # string2<String>:: The string to concatenate.
        #
        # ==== Returns
        # String:: the concatenated string with regexp end caps removed.
        def concat_without_endcaps(string1, string2)
          return nil if !string1 and !string2
          return string1 if string2.nil?
          return string2 if string1.nil?
          s1 = string1[-1] == ?$ ? string1[0..-2] : string1
          s2 = string2[0] == ?^ ? string2[1..-1] : string2
          s1 + s2
        end
 
        # ==== Parameters
        # arr<Array>:: The array to convert to a code string.
        #
        # ==== Returns
        # String::
        # The arr's elements converted to string and joined with " + ", with
        # any string elements surrounded by quotes.
        def array_to_code(arr)
          code = ''
          arr.each_with_index do |part, i|
            code << ' + ' if i > 0
            case part
            when Symbol
              code << part.to_s
            when String
              code << %{"#{part}"}
            else
              raise "Don't know how to compile array part: #{part.class} [#{i}]"
            end
          end
          code
        end
      end # class << self
 
      # ==== Parameters
      # conditions<Hash>::
      # Conditions to be met for this behavior to take effect.
      # params<Hash>::
      # Hash describing the course action to take (Behavior) when the
      # conditions match. The values of the +params+ keys must be Strings.
      # parent<Behavior, Nil>::
      # The parent of this Behavior. Defaults to nil.
      def initialize(conditions = {}, params = {}, parent = nil)
        # Must wait until after deducing placeholders to set @params !
        @conditions, @params, @parent = conditions, {}, parent
        @placeholders = {}
        stringify_conditions
        copy_original_conditions
        deduce_placeholders
        @params.merge! params
      end
 
      # Register a new route.
      #
      # ==== Parameters
      # path<String, Regex>:: The url path to match
      # params<Hash>:: The parameters the new routes maps to.
      #
      # ==== Returns
      # Route:: The resulting Route.
      #---
      # @public
      def add(path, params = {})
        match(path).to(params)
      end
 
      # Matches a +path+ and any number of optional request methods as
      # conditions of a route. Alternatively, +path+ can be a hash of
      # conditions, in which case +conditions+ ignored.
      #
      # ==== Parameters
      #
      # path<String, Regexp>::
      # When passing a string as +path+ you're defining a literal definition
      # for your route. Using a colon, ex.: ":login", defines both a capture
      # and a named param.
      # When passing a regular expression you can define captures explicitly
      # within the regular expression syntax.
      # +path+ is optional.
      # conditions<Hash>::
      # Addational conditions that the request must meet in order to match.
      # the keys must be methods that the Merb::Request instance will respond
      # to. The value is the string or regexp that matched the returned value.
      # Conditions are inherited by child routes.
      # &block::
      # Passes a new instance of a Behavior object into the optional block so
      # that sub-matching and routes nesting may occur.
      #
      # ==== Returns
      # Behavior::
      # A new instance of Behavior with the specified path and conditions.
      #
      # +Tip+: When nesting always make sure the most inner sub-match registers
      # a Route and doesn't just returns new Behaviors.
      #
      # ==== Examples
      #
      # # registers /foo/bar to controller => "foo", :action => "bar"
      # # and /foo/baz to controller => "foo", :action => "baz"
      # r.match "/foo" do |f|
      # f.params[:controller] = 'foo'
      # f.match("/bar").to(:action => "bar")
      # f.match("/baz").to(:action => "caz")
      # end
      #
      # # match also takes regular expressions
      # r.match(%r[/account/([a-z]{4,6})]).to(:controller => "account",
      # :action => "show", :id => "[1]")
      #---
      # @public
      def match(path = '', conditions = {}, &block)
        if path.is_a? Hash
          conditions = path
        else
          conditions[:path] = path
        end
        match_without_path(conditions, &block)
      end
 
      # Generates a new child behavior without the path if the path matches
      # an empty string. Yields the new behavior to a block.
      #
      # ==== Parameters
      # conditions<Hash>:: Optional conditions to pass to the new route.
      #
      # ==== Block parameters
      # new_behavior<Behavior>:: The child behavior.
      #
      # ==== Returns
      # Behavior:: The new behavior.
      def match_without_path(conditions = {})
        new_behavior = self.class.new(conditions, {}, self)
        conditions.delete :path if ['', '^$'].include?(conditions[:path])
        yield new_behavior if block_given?
        new_behavior
      end
 
      # ==== Parameters
      # params<Hash>:: Optional additional parameters for generating the route.
      # &conditional_block:: A conditional block to be passed to Route.new.
      #
      # ==== Returns
      # Route:: A new route based on this behavior.
      def to_route(params = {}, &conditional_block)
        @params.merge! params
        Route.new compiled_conditions, compiled_params, self, &conditional_block
      end
 
      # Combines common case of match being used with
      # to({}).
      #
      # ==== Returns
      # <Route>:: route that uses params from named path segments.
      #
      # ==== Examples
      # r.match!("/api/:token/:controller/:action/:id")
      #
      # is the same thing as
      #
      # r.match!("/api/:token/:controller/:action/:id").to({})
      def match!(path = '', conditions = {}, &block)
        self.match(path, conditions, &block).to({})
      end
 
      # Creates a Route from one or more Behavior objects, unless a +block+ is
      # passed in.
      #
      # ==== Parameters
      # params<Hash>:: The parameters the route maps to.
      # &block::
      # Optional block. A new Behavior object is yielded and further #to
      # operations may be called in the block.
      #
      # ==== Block parameters
      # new_behavior<Behavior>:: The child behavior.
      #
      # ==== Returns
      # Route:: It registers a new route and returns it.
      #
      # ==== Examples
      # r.match('/:controller/:id).to(:action => 'show')
      #
      # r.to :controller => 'simple' do |s|
      # s.match('/test').to(:action => 'index')
      # s.match('/other').to(:action => 'other')
      # end
      #---
      # @public
      def to(params = {}, &block)
        if block_given?
          new_behavior = self.class.new({}, params, self)
          yield new_behavior if block_given?
          new_behavior
        else
          to_route(params).register
        end
      end
 
      # Takes a block and stores it for deferred conditional routes. The block
      # takes the +request+ object and the +params+ hash as parameters.
      #
      # ==== Parameters
      # params<Hash>:: Parameters and conditions associated with this behavior.
      # &conditional_block::
      # A block with the conditions to be met for the behavior to take
      # effect.
      #
      # ==== Returns
      # Route :: The default route.
      #
      # ==== Examples
      # r.defer_to do |request, params|
      # params.merge :controller => 'here',
      # :action => 'there' if request.xhr?
      # end
      #---
      # @public
      def defer_to(params = {}, &conditional_block)
        Router.routes << (route = to_route(params, &conditional_block))
        route
      end
 
      # Creates the most common routes /:controller/:action/:id.format when
      # called with no arguments.
      # You can pass a hash or a block to add parameters or override the default
      # behavior.
      #
      # ==== Parameters
      # params<Hash>::
      # This optional hash can be used to augment the default settings
      # &block::
      # When passing a block a new behavior is yielded and more refinement is
      # possible.
      #
      # ==== Returns
      # Route:: the default route
      #
      # ==== Examples
      #
      # # Passing an extra parameter "mode" to all matches
      # r.default_routes :mode => "default"
      #
      # # specifying exceptions within a block
      # r.default_routes do |nr|
      # nr.defer_to do |request, params|
      # nr.match(:protocol => "http://").to(:controller => "login",
      # :action => "new") if request.env["REQUEST_URI"] =~ /\/private\//
      # end
      # end
      #---
      # @public
      def default_routes(params = {}, &block)
        match(%r{/:controller(/:action(/:id)?)?(\.:format)?}).to(params, &block)
      end
 
      # Creates a namespace for a route. This way you can have logical
      # separation to your routes.
      #
      # ==== Parameters
      # name_or_path<String, Symbol>:: The name or path of the namespace.
      # options<Hash>:: Optional hash, set :path if you want to override what appears on the url
      # &block::
      # A new Behavior instance is yielded in the block for nested resources.
      #
      # ==== Block parameters
      # r<Behavior>:: The namespace behavior object.
      #
      # ==== Examples
      # r.namespace :admin do |admin|
      # admin.resources :accounts
      # admin.resource :email
      # end
      #
      # # /super_admin/accounts
      # r.namespace(:admin, :path=>"super_admin") do |admin|
      # admin.resources :accounts
      # end
      #---
      # @public
      def namespace(name_or_path, options={}, &block)
        path = options[:path] || name_or_path.to_s
        (path.empty? ? self : match("/#{path}")).to(:namespace => name_or_path.to_s) do |r|
          yield r
        end
      end
 
      # Behavior#+resources+ is a route helper for defining a collection of
      # RESTful resources. It yields to a block for child routes.
      #
      # ==== Parameters
      # name<String, Symbol>:: The name of the resources
      # options<Hash>::
      # Ovverides and parameters to be associated with the route
      #
      # ==== Options (options)
      # :namespace<~to_s>: The namespace for this route.
      # :name_prefix<~to_s>:
      # A prefix for the named routes. If a namespace is passed and there
      # isn't a name prefix, the namespace will become the prefix.
      # :controller<~to_s>: The controller for this route
      # :collection<~to_s>: Special settings for the collections routes
      # :member<Hash>:
      # Special settings and resources related to a specific member of this
      # resource.
      # :keys<Array>:
      # A list of the keys to be used instead of :id with the resource in the order of the url.
      #
      # ==== Block parameters
      # next_level<Behavior>:: The child behavior.
      #
      # ==== Returns
      # Array::
      # Routes which will define the specified RESTful collection of resources
      #
      # ==== Examples
      #
      # r.resources :posts # will result in the typical RESTful CRUD
      # # GET /posts/?(\.:format)? :action => "index"
      # # GET /posts/index(\.:format)? :action => "index"
      # # GET /posts/new :action => "new"
      # # POST /posts/?(\.:format)?, :action => "create"
      # # GET /posts/:id(\.:format)? :action => "show"
      # # GET /posts/:id[;/]edit :action => "edit"
      # # PUT /posts/:id(\.:format)? :action => "update"
      # # GET /posts/:id[;/]delete :action => "delete"
      # # DELETE /posts/:id(\.:format)? :action => "destroy"
      #
      # # Nesting resources
      # r.resources :posts do |posts|
      # posts.resources :comments
      # end
      #---
      # @public
      def resources(name, options = {})
        namespace = options[:namespace] || merged_params[:namespace]
 
        next_level = match "/#{name}"
 
        name_prefix = options.delete :name_prefix
        matched_keys = options[:keys] ? options.delete(:keys).map{|k| ":#{k}"}.join("/") : ":id"
 
        if name_prefix.nil? && !namespace.nil?
          name_prefix = namespace_to_name_prefix namespace
        end
 
        unless @@parent_resource.empty?
          parent_resource = namespace_to_name_prefix @@parent_resource.join('_')
        end
 
        options[:controller] ||= merged_params[:controller] || name.to_s
 
        singular = name.to_s.singularize
 
        route_plural_name = "#{name_prefix}#{parent_resource}#{name}"
        route_singular_name = "#{name_prefix}#{parent_resource}#{singular}"
 
        behaviors = []
 
        if member = options.delete(:member)
          member.each_pair do |action, methods|
            behaviors << Behavior.new(
            { :path => %r{^/#{matched_keys}[/;]#{action}(\.:format)?$}, :method => /^(#{[methods].flatten * '|'})$/ },
            { :action => action.to_s }, next_level
            )
            next_level.match("/#{matched_keys}/#{action}").to_route.name(:"#{action}_#{route_singular_name}")
          end
        end
 
        if collection = options.delete(:collection)
          collection.each_pair do |action, methods|
            behaviors << Behavior.new(
            { :path => %r{^[/;]#{action}(\.:format)?$}, :method => /^(#{[methods].flatten * '|'})$/ },
            { :action => action.to_s }, next_level
            )
            next_level.match("/#{action}").to_route.name(:"#{action}_#{route_plural_name}")
          end
        end
 
        routes = many_behaviors_to(behaviors + next_level.send(:resources_behaviors, matched_keys), options)
 
 
 
        # Add names to some routes
        [['', :"#{route_plural_name}"],
        ["/#{matched_keys}", :"#{route_singular_name}"],
        ['/new', :"new_#{route_singular_name}"],
        ["/#{matched_keys}/edit", :"edit_#{route_singular_name}"],
        ["/#{matched_keys}/delete", :"delete_#{route_singular_name}"]
        ].each do |path,name|
          next_level.match(path).to_route.name(name)
        end
 
 
        parent_keys = (matched_keys == ":id") ? ":#{singular}_id" : matched_keys
        if block_given?
          @@parent_resource.push(singular)
          yield next_level.match("/#{parent_keys}")
          @@parent_resource.pop
        end
 
        routes
      end
 
      # Behavior#+resource+ is a route helper for defining a singular RESTful
      # resource. It yields to a block for child routes.
      #
      # ==== Parameters
      # name<String, Symbol>:: The name of the resource.
      # options<Hash>::
      # Overides and parameters to be associated with the route.
      #
      # ==== Options (options)
      # :namespace<~to_s>: The namespace for this route.
      # :name_prefix<~to_s>:
      # A prefix for the named routes. If a namespace is passed and there
      # isn't a name prefix, the namespace will become the prefix.
      # :controller<~to_s>: The controller for this route
      #
      # ==== Block parameters
      # next_level<Behavior>:: The child behavior.
      #
      # ==== Returns
      # Array:: Routes which define a RESTful single resource.
      #
      # ==== Examples
      #
      # r.resources :account # will result in the typical RESTful CRUD
      # # GET /account/new :action => "new"
      # # POST /account/?(\.:format)?, :action => "create"
      # # GET /account/(\.:format)? :action => "show"
      # # GET /account/[;/]edit :action => "edit"
      # # PUT /account/(\.:format)? :action => "update"
      # # GET /account/[;/]delete :action => "delete"
      # # DELETE /account/(\.:format)? :action => "destroy"
      #
      # You can optionally pass :namespace and :controller to refine the routing
      # or pass a block to nest resources.
      #
      # r.resource :account, :namespace => "admin" do |account|
      # account.resources :preferences, :controller => "settings"
      # end
      # ---
      # @public
      def resource(name, options = {})
        namespace = options[:namespace] || merged_params[:namespace]
 
        next_level = match "/#{name}"
 
        options[:controller] ||= merged_params[:controller] || name.to_s
 
        # Do not pass :name_prefix option on to to_resource
        name_prefix = options.delete :name_prefix
 
        if name_prefix.nil? && !namespace.nil?
          name_prefix = namespace_to_name_prefix namespace
        end
 
        unless @@parent_resource.empty?
          parent_resource = namespace_to_name_prefix @@parent_resource.join('_')
        end
 
        routes = next_level.to_resource options
 
        route_name = "#{name_prefix}#{name}"
 
        next_level.match('').to_route.name(:"#{route_name}")
        next_level.match('/new').to_route.name(:"new_#{route_name}")
        next_level.match('/edit').to_route.name(:"edit_#{route_name}")
        next_level.match('/delete').to_route.name(:"delete_#{route_name}")
 
        if block_given?
          @@parent_resource.push(route_name)
          yield next_level
          @@parent_resource.pop
        end<