diff --git a/lib/camping-unabridged.rb b/lib/camping-unabridged.rb index d3aafbc..7d444b9 100644 --- a/lib/camping-unabridged.rb +++ b/lib/camping-unabridged.rb @@ -31,8 +31,9 @@ %w[tempfile uri].map { |l| require l } class Object + # Define a method m with the passed block on the metaclass. def meta_def(m,&b) - (class<r method. A great way to forward your @@ -359,7 +365,7 @@ def to_a;[status, body, headers] end def initialize(r, e, m) #:nodoc: @status, @method, @env, @headers, @root = 200, m.downcase, e, - {'Content-Type'=>'text/html'}, e.SCRIPT_NAME.sub(/\/$/,'') + H['Content-Type','text/html'], e.SCRIPT_NAME.sub(/\/$/,'') @k = C.kp(e.HTTP_COOKIE) q = C.qsp(e.QUERY_STRING) @in = r @@ -682,7 +688,7 @@ def method_missing(m, c, *a) X.M k = X.const_get(c).new(StringIO.new, H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s) - H.new(a.pop).each { |e,f| k.send("#{e}=",f) } if Hash === a[-1] + H[a.pop].each { |e,f| k.send("#{e}=",f) } if Hash === a[-1] k.service(*a) end end diff --git a/lib/camping.rb b/lib/camping.rb index b7709eb..7ca8ce1 100644 --- a/lib/camping.rb +++ b/lib/camping.rb @@ -1,21 +1,24 @@ %w[tempfile uri].map{|l|require l};class Object;def meta_def m,&b -(class<URL(*a)end;def r s,b,h={};@status=s;headers.merge!h +r 302,'','Location'=>URL(*a)end;def r s,b,h={};@status=s;headers.u h @body=b end;def to_a;[status,body,headers]end;def initialize r,e,m -@status,@method,@env,@headers,@root=200,m.downcase,e,{ -'Content-Type'=>"text/html"},e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE +@status,@method,@env,@headers,@root=200,m.downcase,e,H[ +'Content-Type',"text/html"],e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE q=C.qsp e.QUERY_STRING;@in=r;case e.CONTENT_TYPE when %r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n b=/(?:\r?\n|\A)#{Regexp::quote"--#$1"}(?:--)?\r$/;until @@ -49,7 +52,7 @@ def kp s;c=qsp(s,';,')end;def run r=$stdin,e=ENV;X.M;e=H[e.to_hash] k,a=X.D e.PATH_INFO=un("/#{e.PATH_INFO}".gsub(/\/+/,'/'));k.new( r,e,(m=e.REQUEST_METHOD||"GET")).Y.service(*a);rescue=>x;X::ServerError.new( r,e,'get').service(k,m,x)end;def method_missing m,c,*a;X.M;k=X.const_get(c). -new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s);H. -new(a.pop).each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end +new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s) +H[a.pop].each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end module Views;include X,Helpers;end;module Models;autoload:Base,'camping/db';def Y;self;end;end;autoload:Mab,'camping/mab'end diff --git a/test/apps/misc.rb b/test/apps/misc.rb index f30df81..f73bdd2 100644 --- a/test/apps/misc.rb +++ b/test/apps/misc.rb @@ -25,6 +25,9 @@ def get; redirect(RR) end class RR def get; render :rr; end end + class BadLinks + def get; render :bad_links; end + end end module Views @@ -37,6 +40,7 @@ def layout li{ a "render partial", :href=>R(RenderPartial)} li{ a "X-Sendfile", :href=>R(XSendfile)} li{ a "Links", :href=>R(Links)} + li{ a "BadLinks", :href=>R(BadLinks)} li{ a "Redirect", :href=>R(Redirect)} end p { yield } @@ -54,14 +58,22 @@ def index def links a "plain", :href=>R(Links); br - a "with hash and args", :href=>R(Links, "moo", 3, :with=>"Hash"); br + a "with args and hash", :href=>R(Links, "moo", 3, :with=>"Hash"); br + a "with args and mult. hash", :href=>R(Links, "hoi", 8, :with=>"multiple", 3=>"hash"); br # TODO : with R(nil) + a "bad arity", :href=>R(RR, :moo) + a "bad args", :href=>R(Links, 3, "moo") + end + def rr p "got redirected" end + end end