Module | Camping::Controllers |
In: |
lib/camping-unabridged.rb
|
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
There are two ways to define controllers: Just defining a class and let Camping figure out the route, or add the route explicitly using R.
If you don‘t use 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
You have these variables which describes the request:
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
N | = | H.new { |_,x| x.downcase }.merge! "N" => '(\d+)', "X" => '([^/]+)', "Index" => '' | ||
I | = | R() | Internal controller with no route. Used to show internal messages. |
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:
So, define your catch-all controllers last.
# File lib/camping-unabridged.rb, line 565 565: def D(p, m, e) 566: p = '/' if !p || !p[0] 567: a=O[:_t].find{|n,_|n==p} and return [I, :serve, *a] 568: @r.map { |k| 569: k.urls.map { |x| 570: return (k.method_defined?(m)) ? 571: [k, m, *$~[1..-1].map{|x|U.unescape(x)}] : [I, 'r501', m] if p =~ /^#{x}\/?$/ 572: } 573: } 574: [I, 'r404', p] 575: 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 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 591 591: def M 592: def M #:nodoc: 593: end 594: constants.map { |c| 595: k = const_get(c) 596: k.send :include,C,X,Base,Helpers,Models 597: @r=[k]+@r if @r-[k]==@r 598: k.meta_def(:urls){["/#{c.to_s.scan(/.[^A-Z]*/).map(&N.method(:[]))*'/'}"]}if !k.respond_to?:urls 599: } 600: end
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 546 546: def R *u 547: r=@r 548: Class.new { 549: meta_def(:urls){u} 550: meta_def(:inherited){|x|r<<x} 551: } 552: end