Changeset 236
- Timestamp:
- 10/02/2007 09:21:48 (14 months ago)
- Location:
- trunk
- Files:
-
- 3 modified
-
lib/camping-unabridged.rb (modified) (6 diffs)
-
lib/camping.rb (modified) (3 diffs)
-
test/apps/misc.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/camping-unabridged.rb
r235 r236 32 32 33 33 class Object 34 # Define a method m with the passed block on the metaclass. 34 35 def meta_def(m,&b) 35 (class<<self;self end). instance_eval{define_method(m,&b)}36 (class<<self;self end).send(:define_method,m,&b) 36 37 end 37 38 end … … 126 127 end 127 128 alias u merge! 129 undef id, type 128 130 end 129 131 … … 200 202 def R(c,*g) 201 203 p,h=/\(.+?\)/,g.grep(Hash) 202 (g-=h).inject(c.urls.find{|x|x.scan(p).size==g.size}.dup){|s,a| 203 s.sub p,C.escape((a[a.class.primary_key]rescue a)) 204 }+(h.any?? "?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&": "") 204 g-=h 205 raise "bad route" unless u = c.urls.find{|x| 206 break x if x.scan(p).size == g.size && 207 /^#{x}\/?$/ =~ (x=g.inject(x){|x,a| 208 x.sub p,C.escape((a[a.class.primary_key]rescue a))}) 209 } 210 h.any?? u+"?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&": u 205 211 end 206 212 … … 342 348 # redirect "/view/12" 343 349 # 344 def r(s, b, h = {}); @status = s; headers. merge!(h); @body = b; end350 def r(s, b, h = {}); @status = s; headers.u(h); @body = b; end 345 351 346 352 # Turn a controller into an array. This is designed to be used to pipe … … 360 366 def initialize(r, e, m) #:nodoc: 361 367 @status, @method, @env, @headers, @root = 200, m.downcase, e, 362 {'Content-Type'=>'text/html'}, e.SCRIPT_NAME.sub(/\/$/,'')368 H['Content-Type','text/html'], e.SCRIPT_NAME.sub(/\/$/,'') 363 369 @k = C.kp(e.HTTP_COOKIE) 364 370 q = C.qsp(e.QUERY_STRING) … … 683 689 k = X.const_get(c).new(StringIO.new, 684 690 H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s) 685 H .new(a.pop).each { |e,f| k.send("#{e}=",f) } if Hash === a[-1]691 H[a.pop].each { |e,f| k.send("#{e}=",f) } if Hash === a[-1] 686 692 k.service(*a) 687 693 end -
trunk/lib/camping.rb
r235 r236 1 1 %w[tempfile uri].map{|l|require l};class Object;def meta_def m,&b 2 (class<<self;self end). instance_eval{define_method(m,&b)}end end2 (class<<self;self end).send(:define_method,m,&b)end end 3 3 module Camping;C=self;S=IO.read(__FILE__)rescue nil;P="Cam\ping Problem!" 4 4 class H<Hash;def method_missing m,*a 5 m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super end;alias u merge!;end 6 module Helpers def R c,*g;p,h=/\(.+?\)/,g.grep(Hash) 7 (g-=h).inject(c.urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|s.sub p,C. 8 escape((a[a.class.primary_key]rescue a))}+(h.any?? "?"+h[0].map{|x|x.map{|z|C. 9 escape z}*"="}*"&": "")end;def /(p);p[/^\//]?@root+p:p;end;def URL c='/',*a 5 m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super end;alias u merge! 6 undef id, type;end 7 module Helpers def R c,*g;p,h=/\(.+?\)/,g.grep(Hash);g-=h; 8 raise"bad route"unless u=c.urls.find{|x|break x if x.scan(p).size==g.size&& 9 /^#{x}\/?$/=~(x=g.inject(x){|x,a|x.sub p,C.escape((a[a.class.primary_key 10 ]rescue a))})} 11 h.any?? u+"?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&":u end 12 def /(p);p[/^\//]?@root+p:p;end;def URL c='/',*a 10 13 c=R(c,*a)if c.respond_to?:urls;c=self/c;c="//"+@env.HTTP_HOST+c if c[/^\//] 11 14 URI(c) end end;module Base;attr_accessor:input,:cookies,:env,:headers,:body, … … 13 16 m=Mab.new({},self);s=m.capture{send(*a,&b)};s=m.capture{send(:layout){s}}if 14 17 /^_/!~a[0].to_s and m.respond_to?:layout;s end;def redirect*a 15 r 302,'','Location'=>URL(*a)end;def r s,b,h={};@status=s;headers. merge!h18 r 302,'','Location'=>URL(*a)end;def r s,b,h={};@status=s;headers.u h 16 19 @body=b end;def to_a;[status,body,headers]end;def initialize r,e,m 17 @status,@method,@env,@headers,@root=200,m.downcase,e, {18 'Content-Type' =>"text/html"},e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE20 @status,@method,@env,@headers,@root=200,m.downcase,e,H[ 21 'Content-Type',"text/html"],e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE 19 22 q=C.qsp e.QUERY_STRING;@in=r;case e.CONTENT_TYPE 20 23 when %r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n … … 50 53 r,e,(m=e.REQUEST_METHOD||"GET")).Y.service(*a);rescue=>x;X::ServerError.new( 51 54 r,e,'get').service(k,m,x)end;def method_missing m,c,*a;X.M;k=X.const_get(c). 52 new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s) ;H.53 new(a.pop).each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end55 new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s) 56 H[a.pop].each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end 54 57 module Views;include X,Helpers;end;module Models;autoload:Base,'camping/db';def 55 58 Y;self;end;end;autoload:Mab,'camping/mab'end -
trunk/test/apps/misc.rb
r227 r236 26 26 def get; render :rr; end 27 27 end 28 class BadLinks 29 def get; render :bad_links; end 30 end 28 31 end 29 32 … … 38 41 li{ a "X-Sendfile", :href=>R(XSendfile)} 39 42 li{ a "Links", :href=>R(Links)} 43 li{ a "BadLinks", :href=>R(BadLinks)} 40 44 li{ a "Redirect", :href=>R(Redirect)} 41 45 end … … 55 59 def links 56 60 a "plain", :href=>R(Links); br 57 a "with hash and args", :href=>R(Links, "moo", 3, :with=>"Hash"); br 61 a "with args and hash", :href=>R(Links, "moo", 3, :with=>"Hash"); br 62 a "with args and mult. hash", :href=>R(Links, "hoi", 8, :with=>"multiple", 3=>"hash"); br 58 63 # TODO : with <AR::Base object 64 end 65 66 def bad_links 67 a "null controller", :href=>R(nil) 68 a "bad arity", :href=>R(RR, :moo) 69 a "bad args", :href=>R(Links, 3, "moo") 59 70 end 60 71 … … 62 73 p "got redirected" 63 74 end 75 64 76 65 77 end
