Changeset 236

Show
Ignore:
Timestamp:
10/02/2007 09:21:48 (14 months ago)
Author:
zimbatm
Message:

micro-optimizations and fix for ticket #96 : more strictness for Helpers#R()

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/camping-unabridged.rb

    r235 r236  
    3232 
    3333class Object 
     34  # Define a method m with the passed block on the metaclass. 
    3435  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) 
    3637  end 
    3738end 
     
    126127    end 
    127128    alias u merge! 
     129    undef id, type 
    128130  end 
    129131 
     
    200202    def R(c,*g) 
    201203      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 
    205211    end 
    206212 
     
    342348    #   redirect "/view/12" 
    343349    # 
    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 
    345351 
    346352    # Turn a controller into an array.  This is designed to be used to pipe 
     
    360366    def initialize(r, e, m) #:nodoc: 
    361367      @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(/\/$/,'') 
    363369      @k = C.kp(e.HTTP_COOKIE) 
    364370      q = C.qsp(e.QUERY_STRING) 
     
    683689      k = X.const_get(c).new(StringIO.new, 
    684690             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] 
    686692      k.service(*a) 
    687693    end 
  • trunk/lib/camping.rb

    r235 r236  
    11%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 
    33module Camping;C=self;S=IO.read(__FILE__)rescue nil;P="Cam\ping Problem!" 
    44class 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 
     5m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super end;alias u merge! 
     6undef id, type;end 
     7module Helpers def R c,*g;p,h=/\(.+?\)/,g.grep(Hash);g-=h; 
     8raise"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))})} 
     11h.any?? u+"?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&":u end 
     12def /(p);p[/^\//]?@root+p:p;end;def URL c='/',*a 
    1013c=R(c,*a)if c.respond_to?:urls;c=self/c;c="//"+@env.HTTP_HOST+c if c[/^\//] 
    1114URI(c) end end;module Base;attr_accessor:input,:cookies,:env,:headers,:body, 
     
    1316m=Mab.new({},self);s=m.capture{send(*a,&b)};s=m.capture{send(:layout){s}}if 
    1417/^_/!~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 
     18r 302,'','Location'=>URL(*a)end;def r s,b,h={};@status=s;headers.u h 
    1619@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 
    1922q=C.qsp e.QUERY_STRING;@in=r;case e.CONTENT_TYPE 
    2023when %r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n 
     
    5053r,e,(m=e.REQUEST_METHOD||"GET")).Y.service(*a);rescue=>x;X::ServerError.new( 
    5154r,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 
     55new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s) 
     56H[a.pop].each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end 
    5457module Views;include X,Helpers;end;module Models;autoload:Base,'camping/db';def 
    5558Y;self;end;end;autoload:Mab,'camping/mab'end 
  • trunk/test/apps/misc.rb

    r227 r236  
    2626      def get; render :rr; end 
    2727    end 
     28    class BadLinks 
     29      def get; render :bad_links; end 
     30    end 
    2831  end 
    2932 
     
    3841            li{ a "X-Sendfile", :href=>R(XSendfile)} 
    3942            li{ a "Links", :href=>R(Links)} 
     43            li{ a "BadLinks", :href=>R(BadLinks)} 
    4044            li{ a "Redirect", :href=>R(Redirect)} 
    4145          end 
     
    5559    def links 
    5660      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 
    5863      # 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") 
    5970    end 
    6071 
     
    6273      p "got redirected" 
    6374    end 
     75 
    6476 
    6577  end