Module: Camping::Controllers::Base
Overview
Controllers::Base is built into each controller by way of the generic routing class Controllers::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.
Treating controller methods like Response objects
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
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#input ⇒ Object
Returns the value of attribute input.
-
#root ⇒ Object
Returns the value of attribute root.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
-
#method_missing(m, *args, &blk) ⇒ Object
Any stray method calls will be passed to Markaby.
-
#r(s, b, h = {}) ⇒ Object
A quick means of setting this controller’s status, body and headers.
-
#redirect(c, *args) ⇒ Object
Formulate a redirect response: a 302 status with
Location
header and a blank body. -
#render(m) ⇒ Object
Display a view, calling it by its method name
m
. -
#service(r, e, m, a) ⇒ Object
:nodoc:.
-
#to_s ⇒ Object
:nodoc:.
Methods included from Helpers
#/, #R, #errors_for
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &blk) ⇒ Object
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.
179 180 181 |
# File 'lib/camping-unabridged.rb', line 179 def method_missing(m,*args,&blk);str=m==:render ? markaview(*args, &blk):eval("markaby.#{m}(*args,&blk)");str=markaview(:layout){str}rescue nil;r( 200,str.to_s);end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
7 8 9 |
# File 'lib/camping.rb', line 7 def body @body end |
#cookies ⇒ Object
Returns the value of attribute cookies.
7 8 9 |
# File 'lib/camping.rb', line 7 def @cookies end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/camping.rb', line 7 def headers @headers end |
#input ⇒ Object
Returns the value of attribute input.
7 8 9 |
# File 'lib/camping.rb', line 7 def input @input end |
#root ⇒ Object
Returns the value of attribute root.
7 8 9 |
# File 'lib/camping.rb', line 7 def root @root end |
#status ⇒ Object
Returns the value of attribute status.
7 8 9 |
# File 'lib/camping.rb', line 7 def status @status end |
Instance Method Details
#r(s, b, h = {}) ⇒ Object
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"
209 |
# File 'lib/camping-unabridged.rb', line 209 def r(s,b,h={});@status=s;@headers.merge!(h);@body=b;end |
#redirect(c, *args) ⇒ Object
Formulate a redirect response: a 302 status with Location
header and a blank body. If c
is a string, the root path will be added. If c
is a controller class, Helpers::R will be used to route the redirect and the root path will be added.
So, given a root of /articles
:
redirect "view/12" # redirects to "/articles/view/12"
redirect View, 12 # redirects to "/articles/view/12"
195 196 197 |
# File 'lib/camping-unabridged.rb', line 195 def redirect(c,*args);c=R(c,*args)if c.respond_to?:urls;r(302,'','Location'=>self/c) end |
#render(m) ⇒ Object
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
166 |
# File 'lib/camping-unabridged.rb', line 166 def render(m); end |
#service(r, e, m, a) ⇒ Object
:nodoc:
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/camping-unabridged.rb', line 211 def service(r,e,m,a)@status,@headers,@root=200,{},e['SCRIPT_NAME'];cook=C.kp( e['HTTP_COOKIE']);qs=C.qs_parse(e['QUERY_STRING']);if "POST"==m;inp=r.read(e[ 'CONTENT_LENGTH'].to_i);if %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)|n. match(e['CONTENT_TYPE']);b="--#$1";inp.split(/(?:\r?\n|\A)#{Regexp::quote( b)}(?:--)?\r\n/m).each{|pt|h,v=pt.split("\r\n\r\n",2);fh={};[:name,:filename]. each{|x|fh[x]=$1 if h=~/^Content-Disposition: form-data;.*(?:\s#{x}="([^"]+)")\ /m};fn=fh[:name];if fh[:filename];fh[:type]=$1 if h =~ /^Content-Type: (.+?)(\ \r\n|\Z)/m;fh[:tempfile]=Tempfile.new("#{C}").instance_eval{binmode;write v rewind;self};else;fh=v;end;qs[fn]=fh if fn};else;qs.merge!(C.qs_parse(inp));end end;@cookies,@input=[cook,qs].map{|_|OpenStruct.new(_)};@body=method(m.downcase ).call(*a);@headers["Set-Cookie"]=@cookies.marshal_dump.map{|k,v|"#{k}=#{C. escape(v)}; path=/" if v != cook[k]}.compact;self;end |
#to_s ⇒ Object
:nodoc:
243 244 245 |
# File 'lib/camping-unabridged.rb', line 243 def to_s;"Status: #{ @status}\n#{{'Content-Type'=>'text/html'}.merge(@headers).map{|k,v|v.to_a.map{ |v2|"#{k}: #{v2}"}}.flatten.join("\n")}\n\n#{@body}";end |