module Camping::X

Controllers receive the requests and sends a response back to the client. A controller is simply a class which must implement the HTTP methods it wants to accept:

module Nuts::Controllers
  class Index
    def get
      "Hello World"
    end
  end

  class Posts
    def post
      Post.create(@input) 
      redirect Index
    end
  end  
end

Defining a controller

There are two ways to define controllers: Just defining a class and let Camping figure out the route, or add the route explicitly using Camping::Controllers.R.

If you don’t use Camping::Controllers.R, Camping will first split the controller name up by words (HelloWorld => Hello and World). Then it would do the following:

Here’s a few examples:

Index   # => /
PostN   # => /post/(\d+)
PageX   # => /page/([^/]+)
Pages   # => /pages

The request

You have these variables which describes the request:

The response

You can change these variables to your needs:

If you haven’t set @body, it will use the return value of the method:

module Nuts::Controllers
  class Index
    def get
      "This is the body"
    end
  end

  class Posts
    def get
      @body = "Hello World!"
      "This is ignored"
    end
  end
end

Constants

I

Internal controller with no route. Used to show internal messages.

N

Public Class Methods

D(p, m, e) click to toggle source

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.

# File lib/camping-unabridged.rb, line 567
def D(p, m, e)
  p = '/' if !p || !p[0]
  a=O[:_t].find{|n,_|n==p} and return [I, :serve, *a]
  @r.map { |k|
    k.urls.map { |x|
      return (k.method_defined?(m)) ?
        [k, m, *$~[1..-1].map{|x|U.unescape(x)}] : [I, 'r501', m] if p =~ /^#{x}\/?$/
    }
  }
  [I, 'r404', p]
end
M() click to toggle source

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 to set things up. Don’t call it too early though. Any controllers added after this method is called won’t work properly

# File lib/camping-unabridged.rb, line 593
def M
  def M #:nodoc:
  end
R(*u) click to toggle source

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

The route is a regexp which will match the request path. Anything enclosed in parenthesis will be sent to the method as arguments.

module Camping::Controllers
  class Edit < R '/edit/(\d+)', '/new'
    def get(id)
      if id   # edit
      else    # new
      end
    end
  end
end
# File lib/camping-unabridged.rb, line 548
def R *u
  r=@r
  Class.new {
    meta_def(:urls){u}
    meta_def(:inherited){|x|r<<x}
  }
end