Module: Camping::Controllers

Included in:
Views
Defined in:
lib/camping-unabridged.rb,
lib/camping.rb

Overview

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.

Defined Under Namespace

Modules: Base Classes: NotFound, R, ServerError

Class Method Summary collapse

Class Method Details

.D(path) ⇒ Object

Dispatch routes to controller classes. Classes are searched in no particular order. 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.



329
330
331
# File 'lib/camping-unabridged.rb', line 329

def D(path);constants.inject(nil){|d,c|k=
const_get(c);k.meta_def(:urls){["/#{c.downcase}"]}if !(k<R);d||([k, $~[1..-1]
] if k.urls.find { |x| path =~ /^#{x}\/?$/ })}||[NotFound, [path]];end

.R(*urls) ⇒ Object

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.



323
324
# File 'lib/camping-unabridged.rb', line 323

def R(*urls);Class.new(R){meta_def(:inherited){|c|c.
meta_def(:urls){urls}}};end