| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Camping
Revision: 236
Author: zimbatm
Date: 02 Oct 2007 18:21:48
Diff at Trac: http://code.whytheluckystiff.net/camping/changeset/236
Changes:micro-optimizations and fix for ticket #96 : more strictness for Helpers#R()
Files:| ... | ...@@ -25,6 +25,9 @@ | |
| 25 | 25 | class RR |
| 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 | |
| 30 | 33 | module Views |
| ... | ...@@ -37,6 +40,7 @@ | |
| 37 | 40 | li{ a "render partial", :href=>R(RenderPartial)} |
| 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 |
| 42 | 46 | p { yield } |
| ... | ...@@ -54,14 +58,22 @@ | |
| 54 | 58 | |
| 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 |
| 59 | 64 | end |
| 60 | 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") | |
| 70 | end | |
| 71 | ||
| 61 | 72 | def rr |
| 62 | 73 | p "got redirected" |
| 63 | 74 | end |
| 64 | 75 | |
| 76 | ||
| 65 | 77 | end |
| 66 | 78 | end |
| 67 | 79 |
| ... | ...@@ -1,21 +1,24 @@ | |
| 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 end | |
| 2 | (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, |
| 12 | 15 | :status,:root;Z="\r\n";def method_missing*a,&b;a.shift if a[0]==:render |
| 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!h | |
| 18 | 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_COOKIE | |
| 20 | @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 |
| 21 | 24 | b=/(?:\r?\n|\A)#{Regexp::quote"--#$1"}(?:--)?\r$/;until |
| ... | ...@@ -49,7 +52,7 @@ | |
| 49 | 52 | k,a=X.D e.PATH_INFO=un("/#{e.PATH_INFO}".gsub(/\/+/,'/'));k.new( |
| 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;end | |
| 55 | 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 |
| ... | ...@@ -31,8 +31,9 @@ | |
| 31 | 31 | %w[tempfile uri].map { |l| require l } |
| 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 |
| 38 | 39 | |
| ... | ...@@ -125,6 +126,7 @@ | |
| 125 | 126 | m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super |
| 126 | 127 | end |
| 127 | 128 | alias u merge! |
| 129 | undef id, type | |
| 128 | 130 | end |
| 129 | 131 | |
| 130 | 132 | # Helpers contains methods available in your controllers and views. You may add |
| ... | ...@@ -199,9 +201,13 @@ | |
| 199 | 201 | # |
| 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 | |
| 207 | 213 | # Simply builds a complete path from a path +p+ within the app. If your application is |
| ... | ...@@ -341,7 +347,7 @@ | |
| 341 | 347 | # |
| 342 | 348 | # redirect "/view/12" |
| 343 | 349 | # |
| 344 | def r(s, b, h = {}); @status = s; headers.merge!(h); @body = b; end | |
| 350 | 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 |
| 347 | 353 | # controllers into the <tt>r</tt> method. A great way to forward your |
| ... | ...@@ -359,7 +365,7 @@ | |
| 359 | 365 | |
| 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) |
| 365 | 371 | @in = r |
| ... | ...@@ -682,7 +688,7 @@ | |
| 682 | 688 | X.M |
| 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 |
| 688 | 694 | end |