Changeset 129

Show
Ignore:
Timestamp:
07/10/2006 23:19:51 (2 years ago)
Author:
why
Message:
  • lib/camping.rb: trimmed down all the Markaby hooks to assign the Camping controller as the helper. That, along with some other cleanup, shaved about 200 bytes off. good riddance and no loss of features.
Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/examples/blog.rb

    r112 r129  
    190190    def add 
    191191      if @user 
    192         _form(post, :action => R(Add)) 
     192        _form(@post, :action => R(Add)) 
    193193      else 
    194194        _login 
     
    198198    def edit 
    199199      if @user 
    200         _form(post, :action => R(Edit)) 
     200        _form(@post, :action => R(Edit)) 
    201201      else 
    202202        _login 
     
    205205 
    206206    def view 
    207         _post(post) 
     207        _post(@post) 
    208208 
    209209        p "Comment for this post:" 
     
    241241      p do 
    242242        a "Edit", :href => R(Edit, post) 
     243        text " | " 
    243244        a "View", :href => R(View, post) 
    244245      end 
  • trunk/lib/camping-unabridged.rb

    r128 r129  
    657657      i(k).new(nil,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s).service(*a) 
    658658    end 
    659  
    660     def i k 
    661       def k.urls;["/#{name.downcase}"]end if !k.respond_to? :urls 
    662       k.send(:include, C, Base, Models) if !(k<C) 
    663       k 
    664     end 
    665659  end 
    666660 
  • trunk/lib/camping.rb

    r128 r129  
    1 %w[active_record markaby metaid tempfile uri].each{|l|require l} 
     1%w[active_record markaby metaid tempfile uri].map{|l|require l} 
    22module Camping;Apps=[];C=self;S=IO.read(__FILE__).sub(/S=I.+$/,'') 
    33P="Cam\ping Problem!";module Helpers;def R c,*g;p=/\(.+?\)/;g.inject(c. 
    4 urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|s.sub(p,C.escape((a. 
    5 __send__(a.class.primary_key)rescue a)))}end;def URL c='/',*a;c=R(c,*a)if c. 
     4urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|s.sub p,C.escape((a[ 
     5a.class.primary_key]rescue a))}end;def URL c='/',*a;c=R(c,*a)if c. 
    66respond_to?:urls;c=self/c;c="//"+@env.HTTP_HOST+c if c[/^\//];URI(c) end;def / p 
    77p[/^\//]?@root+p : p end;def errors_for o;ul.errors{o.errors.each_full{|x|li x} 
    88}if o.errors.any?end end;module Base;include Helpers;attr_accessor :input, 
    9 :cookies,:env,:headers,:body,:status,:root;def method_missing m,*a,&b 
    10 s=m==:render ? markaview(*a,&b):eval("markaby.#{m}(*a,&b)");s=markaview(:layout 
    11 ){s} if Views.method_defined?:layout;s end;def r s,b,h={};@status=s;@headers. 
    12 merge! h;@body=b end;def redirect *a;r 302,'','Location'=>URL(*a)end;def 
    13 initialize r,e,m;e=H[e.to_hash];@status,@method,@env,@headers,@root=200,m. 
     9:cookies,:env,:headers,:body,:status,:root;def method_missing *a,&b 
     10a.shift if a[0]==:render;m=markaby;s=m.capture{send(*a,&b)};s=m.layout{s} if 
     11m.respond_to?:layout;s end;def r s,b,h={};@status=s;@headers. 
     12merge! h;@body=b end;def redirect *a;r 302,'','Location'=>URL(*a)end;Z="\r\n" 
     13def initialize r,e,m;e=H[e.to_hash];@status,@method,@env,@headers,@root=200,m. 
    1414downcase,e,{'Content-Type'=>"text/html"},e.SCRIPT_NAME.sub(/\/$/,'') 
    1515@k=C.kp e.HTTP_COOKIE;q=C.qs_parse e.QUERY_STRING;@in=r 
    1616if %r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n.match e.CONTENT_TYPE;b= 
    1717/(?:\r?\n|\A)#{Regexp::quote("--#$1")}(?:--)?\r$/;until @in.eof?;fh=H[];for l in 
    18 @in;case l;when "\r\n":break;when /^Content-Disposition: form-data;/:fh.u H[*$'. 
     18@in;case l;when Z;break;when /^Content-D.+?: form-data;/:fh.u H[*$'. 
    1919scan(/(?:\s(\w+)="([^"]+)")/).flatten];when /^Content-Type: (.+?)(\r$|\Z)/m;fh[ 
    2020:type]=$1;end;end;fn=fh[:name];o=if fh[:filename];o=fh[:tempfile]=Tempfile.new(:C) 
    21 o.binmode;else;fh="";end;while l=@in.read(16384);if l=~b;o<<$`.chomp;@in.seek(-$'. 
     21o.binmode;else;fh=""end;while l=@in.read(16384);if l=~b;o<<$`.chomp;@in.seek(-$'. 
    2222size,IO::SEEK_CUR);break;end;o<<l;end;q[fn]=fh if fn;fh[:tempfile].rewind if 
    2323fh.is_a?H;end;elsif @method=="post";q.u C.qs_parse(@in.read) end;@cookies,@input= 
    2424@k.dup,q.dup end;def service *a;@body=send(@method,*a)if respond_to?@method 
    2525@headers["Set-Cookie"]=@cookies.map{|k,v|"#{k}=#{C.escape(v)}; path=#{self/"/"}\ 
    26 " if v != @k[k]}.compact;self end;def to_s;"Status: #{@status}\r\n#{@headers.map{ 
    27 |k,v|[*v].map{|x|"#{k}: #{x}"}}*"\r\n"}\r\n\r\n#{@body}" end;def markaby;Mab.new( 
    28 instance_variables.map{|iv|[iv[1..-1],instance_variable_get(iv)]}) end;def  
    29 markaview m,*a,&b;h=markaby;h.send m,*a,&b;h.to_s end end;X=module Controllers 
     26" if v!=@k[k]}-[nil];self end;def to_s;"Status: #{@status}#{Z+@headers.map{ 
     27|k,v|[*v].map{|x|"#{k}: #{x}"}}*Z+Z*2+@body}" end;def markaby;Mab.new({},self) 
     28end end;X=module Controllers 
    3029@r=[];class<<self;def r;@r;end;def R *u;r=@r;Class.new{meta_def(:urls){u} 
    31 meta_def(:inherited){|x|r<<x}}end;def M;constants.each{|c|k=const_get(c);k.send( 
    32 :include,C,Base,Models);if !r.include?k;k.meta_def(:urls){["/#{c.downcase}"]} 
    33 r<<k;end}end;def D p;r.each{|k|case p when *k.urls.map{|x|/^#{x}\/?$/};return k, 
    34 $~[1..-1]end};[NotFound, [p]]end end;class NotFound<R();def get p;r(404,Mab.new{h1 P 
     30meta_def(:inherited){|x|r<<x}}end;def M;constants.map{|c|k=const_get(c);k. 
     31send:include,C,Base,Models;if !r.include?k;k.meta_def(:urls){["/#{c.downcase}"]} 
     32r[0,0]=k;end}end;def D p;r.map{|k|k.urls.map{|x|return k,$~[1..-1]if p=~/^#{x}\/?$/} 
     33};[NotFound, [p]]end end;class NotFound<R();def get p;r(404,Mab.new{h1 P 
    3534h2 p+" not found"}) end end;class ServerError<R();def get k,m,e;r(500, 
    3635Mab.new{h1 P;h2 "#{k}.#{m}";h3 "#{e.class} #{e.message}:";ul{ 
    3736e.backtrace.each{|bt|li(bt)}}}.to_s)end end;self;end;class<<self;def goes m 
    38 eval(S.gsub(/Camping/,m.to_s),TOPLEVEL_BINDING);Apps<<const_get(m);end 
     37eval S.gsub(/Camping/,m.to_s),TOPLEVEL_BINDING;Apps<<const_get(m);end 
    3938def escape(s)s.to_s.gsub(/[^ \w.-]+/n){'%'+($&.unpack('H2'*$&.size)*'%').upcase}.tr(' ','+')end 
    4039def un(s)s.tr('+',' ').gsub(/%([\da-f]{2})/in){[$1].pack('H*')} end