Module | Camping::Base |
In: |
lib/camping-unabridged.rb
|
Camping::Base is built into each controller by way of the generic routing class Camping::R. In some ways, this class is trying to do too much, but it saves code for all the glue to stay in one place.
Forgivable, considering that it‘s only really a handful of methods and accessors.
Camping originally came with a barebones Response object, but it‘s often much more readable to just use your controller as the response.
Go ahead and alter the status, cookies, headers and body instance variables as you see fit in order to customize the response.
module Camping::Controllers class SoftLink def get redirect "/" end end end
Is equivalent to:
module Camping::Controllers class SoftLink def get @status = 302 @headers['Location'] = "/" end end end
Z | = | "\r\n" |
body | [RW] | |
cookies | [RW] | |
env | [RW] | |
headers | [RW] | |
input | [RW] | |
root | [RW] | |
status | [RW] |
Any stray method calls will be passed to Markaby. This means you can reply with HTML directly from your controller for quick debugging.
module Camping::Controllers class Info def get; code @env.inspect end end end
If you have a layout method in Camping::Views, it will be used to wrap the HTML.
# File lib/camping-unabridged.rb, line 336 336: def method_missing(*a,&b) 337: a.shift if a[0]==:render 338: m=Mab.new({},self) 339: s=m.capture{send(*a,&b)} 340: s=m.layout{s} if /^_/!~a[0].to_s and m.respond_to?:layout 341: s 342: end
A quick means of setting this controller‘s status, body and headers. Used internally by Camping, but… by all means…
r(302, '', 'Location' => self / "/view/12")
Is equivalent to:
redirect "/view/12"
# File lib/camping-unabridged.rb, line 369 369: def r(s, b, h = {}); @status = s; @headers.merge!(h); @body = b; end
Formulate a redirect response: a 302 status with Location header and a blank body. Uses Helpers#URL to build the location from a controller route or path.
So, given a root of localhost:3301/articles:
redirect "view/12" # redirects to "//localhost:3301/articles/view/12" redirect View, 12 # redirects to "//localhost:3301/articles/view/12"
NOTE: This method doesn‘t magically exit your methods and redirect. You‘ll need to return redirect(…) if this isn‘t the last statement in your code.
# File lib/camping-unabridged.rb, line 356 356: def redirect(*a) 357: r(302,'','Location'=>URL(*a)) 358: end
Display a view, calling it by its method name m. If a layout method is found in Camping::Views, it will be used to wrap the HTML.
module Camping::Controllers class Show def get @posts = Post.find :all render :index end end end
# File lib/camping-unabridged.rb, line 323 323: def render(m); end
All requests pass through this method before going to the controller. Some magic in Camping can be performed by overriding this method.
See code.whytheluckystiff.net/camping/wiki/BeforeAndAfterOverrides for more on before and after overrides with Camping.
# File lib/camping-unabridged.rb, line 421 421: def service(*a) 422: @body = send(@method, *a) if respond_to? @method 423: @headers['Set-Cookie'] = @cookies.map { |k,v| "#{k}=#{C.escape(v)}; path=#{self/"/"}" if v != @k[k] } - [nil] 424: self 425: end
Used by the web server to convert the current request to a string. If you need to alter the way Camping builds HTTP headers, consider overriding this method.
# File lib/camping-unabridged.rb, line 429 429: def to_s 430: "Status: #{@status}#{Z+@headers.map{|k,v|[*v].map{|x|"#{k}: #{x}"}}*Z+Z*2+@body}" 431: end