Changeset 129
- Timestamp:
- 07/10/2006 23:19:51 (2 years ago)
- Location:
- trunk
- Files:
-
- 3 modified
-
examples/blog.rb (modified) (4 diffs)
-
lib/camping-unabridged.rb (modified) (1 diff)
-
lib/camping.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/examples/blog.rb
r112 r129 190 190 def add 191 191 if @user 192 _form( post, :action => R(Add))192 _form(@post, :action => R(Add)) 193 193 else 194 194 _login … … 198 198 def edit 199 199 if @user 200 _form( post, :action => R(Edit))200 _form(@post, :action => R(Edit)) 201 201 else 202 202 _login … … 205 205 206 206 def view 207 _post( post)207 _post(@post) 208 208 209 209 p "Comment for this post:" … … 241 241 p do 242 242 a "Edit", :href => R(Edit, post) 243 text " | " 243 244 a "View", :href => R(View, post) 244 245 end -
trunk/lib/camping-unabridged.rb
r128 r129 657 657 i(k).new(nil,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s).service(*a) 658 658 end 659 660 def i k661 def k.urls;["/#{name.downcase}"]end if !k.respond_to? :urls662 k.send(:include, C, Base, Models) if !(k<C)663 k664 end665 659 end 666 660 -
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} 2 2 module Camping;Apps=[];C=self;S=IO.read(__FILE__).sub(/S=I.+$/,'') 3 3 P="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.4 urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|s.sub p,C.escape((a[ 5 a.class.primary_key]rescue a))}end;def URL c='/',*a;c=R(c,*a)if c. 6 6 respond_to?:urls;c=self/c;c="//"+@env.HTTP_HOST+c if c[/^\//];URI(c) end;def / p 7 7 p[/^\//]?@root+p : p end;def errors_for o;ul.errors{o.errors.each_full{|x|li x} 8 8 }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,&b10 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; def13 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 10 a.shift if a[0]==:render;m=markaby;s=m.capture{send(*a,&b)};s=m.layout{s} if 11 m.respond_to?: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;Z="\r\n" 13 def initialize r,e,m;e=H[e.to_hash];@status,@method,@env,@headers,@root=200,m. 14 14 downcase,e,{'Content-Type'=>"text/html"},e.SCRIPT_NAME.sub(/\/$/,'') 15 15 @k=C.kp e.HTTP_COOKIE;q=C.qs_parse e.QUERY_STRING;@in=r 16 16 if %r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n.match e.CONTENT_TYPE;b= 17 17 /(?:\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[*$'. 19 19 scan(/(?:\s(\w+)="([^"]+)")/).flatten];when /^Content-Type: (.+?)(\r$|\Z)/m;fh[ 20 20 :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(-$'.21 o.binmode;else;fh=""end;while l=@in.read(16384);if l=~b;o<<$`.chomp;@in.seek(-$'. 22 22 size,IO::SEEK_CUR);break;end;o<<l;end;q[fn]=fh if fn;fh[:tempfile].rewind if 23 23 fh.is_a?H;end;elsif @method=="post";q.u C.qs_parse(@in.read) end;@cookies,@input= 24 24 @k.dup,q.dup end;def service *a;@body=send(@method,*a)if respond_to?@method 25 25 @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) 28 end end;X=module Controllers 30 29 @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 P30 meta_def(:inherited){|x|r<<x}}end;def M;constants.map{|c|k=const_get(c);k. 31 send:include,C,Base,Models;if !r.include?k;k.meta_def(:urls){["/#{c.downcase}"]} 32 r[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 35 34 h2 p+" not found"}) end end;class ServerError<R();def get k,m,e;r(500, 36 35 Mab.new{h1 P;h2 "#{k}.#{m}";h3 "#{e.class} #{e.message}:";ul{ 37 36 e.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);end37 eval S.gsub(/Camping/,m.to_s),TOPLEVEL_BINDING;Apps<<const_get(m);end 39 38 def escape(s)s.to_s.gsub(/[^ \w.-]+/n){'%'+($&.unpack('H2'*$&.size)*'%').upcase}.tr(' ','+')end 40 39 def un(s)s.tr('+',' ').gsub(/%([\da-f]{2})/in){[$1].pack('H*')} end
