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.
-
#env ⇒ Object
Returns the value of attribute env.
-
#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
-
#markaby ⇒ Object
:nodoc:.
-
#markaview(m, *a, &b) ⇒ Object
:nodoc:.
-
#method_missing(m, *a, &b) ⇒ 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, *a, &b) ⇒ 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.
283 284 285 |
# File 'lib/camping-unabridged.rb', line 283 def method_missing(m,*a,&b);str=m==:render ? markaview(*a, &b):eval("markaby.#{m}(*a,&b)");str=markaview(:layout){str} if Views.method_defined? :layout;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 |
#env ⇒ Object
Returns the value of attribute env.
7 8 9 |
# File 'lib/camping.rb', line 7 def env @env 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
#markaby ⇒ Object
:nodoc:
350 351 |
# File 'lib/camping-unabridged.rb', line 350 def markaby;Mab.new( instance_variables.map{|iv|[iv[1..-1],instance_variable_get(iv)]});end |
#markaview(m, *a, &b) ⇒ Object
:nodoc:
354 355 356 |
# File 'lib/camping-unabridged.rb', line 354 def markaview(m,*a,&b);h=markaby;h.send(m,*a,&b);h.to_s end |
#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"
313 |
# File 'lib/camping-unabridged.rb', line 313 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"
299 300 301 |
# File 'lib/camping-unabridged.rb', line 299 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
270 |
# File 'lib/camping-unabridged.rb', line 270 def render(m); end |
#service(r, e, m, a) ⇒ Object
:nodoc:
315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/camping-unabridged.rb', line 315 def service(r,e,m,a)@status,@env,@headers,@root=200,e,{'Content-Type'=>'text/html'},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.dup, qs.dup;@body=send(m,*a) if respond_to? m;@headers["Set-Cookie"]=@cookies.map{|k,v|"#{k}=#{C. escape(v)}; path=#{self/"/"}" if v != cook[k]}.compact;self;end |
#to_s ⇒ Object
:nodoc:
347 348 349 |
# File 'lib/camping-unabridged.rb', line 347 def to_s;"Status: #{ @status}\n#{@headers.map{|k,v|[*v].map{ |x|"#{k}: #{x}"}*"\n"}*"\n"}\n\n#{@body}";end |