Module Camping::Controllers
In: lib/camping-unabridged.rb

Controllers is a module for placing classes which handle URLs. This is done by defining a route to each class using the Controllers::R method.

  module Camping::Controllers
    class Edit < R '/edit/(\d+)'
      def get; end
      def post; end
    end
  end

If no route is set, Camping will guess the route from the class name. The rule is very simple: the route becomes a slash followed by the lowercased class name. See Controllers::D for the complete rules of dispatch.

Special classes

There are two special classes used for handling 404 and 500 errors. The NotFound class handles URLs not found. The ServerError class handles exceptions uncaught by your application.

Methods

D   M   R  

Classes and Modules

Class Camping::Controllers::NotFound
Class Camping::Controllers::ServerError

Public Class methods

Dispatch routes to controller classes. For each class, routes are checked for a match based on their order in the routing list given to Controllers::R. If no routes were given, the dispatcher uses a slash followed by the name of the controller lowercased.

Controllers are searched in this order:

# Classes without routes, since they refer to a very specific URL. # Classes with routes are searched in order of their creation.

So, define your catch-all controllers last.

[Source]

     # File lib/camping-unabridged.rb, line 498
498:       def D(path)
499:         r.map { |k|
500:           k.urls.map { |x|
501:             return k, $~[1..-1] if path =~ /^#{x}\/?$/
502:           }
503:         }
504:         [NotFound, [path]]
505:       end

The route maker, this is called by Camping internally, you shouldn‘t need to call it.

Still, it‘s worth know what this method does. Since Ruby doesn‘t keep track of class creation order, we‘re keeping an internal list of the controllers which inherit from R(). This method goes through and adds all the remaining routes to the beginning of the list and ensures all the controllers have the right mixins.

Anyway, if you are calling the URI dispatcher from outside of a Camping server, you‘ll definitely need to call this at least once to set things up.

[Source]

     # File lib/camping-unabridged.rb, line 516
516:       def M
517:         def M #:nodoc:
518:         end
519:         constants.map { |c|
520:           k=const_get(c)
521:           k.send:include,C,Base,Models
522:           r[0,0]=k if !r.include?k
523:           k.meta_def(:urls){["/#{c.downcase}"]}if !k.respond_to?:urls
524:         }
525:       end

Add routes to a controller class by piling them into the R method.

  module Camping::Controllers
    class Edit < R '/edit/(\d+)', '/new'
      def get(id)
        if id   # edit
        else    # new
        end
      end
    end
  end

You will need to use routes in either of these cases:

  • You want to assign multiple routes to a controller.
  • You want your controller to receive arguments.

Most of the time the rules inferred by dispatch method Controllers::D will get you by just fine.

[Source]

     # File lib/camping-unabridged.rb, line 479
479:       def R *u
480:         r=@r
481:         Class.new {
482:           meta_def(:urls){u}
483:           meta_def(:inherited){|x|r<<x}
484:         }
485:       end

[Validate]