GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: the 4k pocket full-of-gags web microframework
Homepage: http://code.whytheluckystiff.net/camping/
Clone URL: git://github.com/why/camping.git
micro-optimizations and fix for ticket #96 : more strictness for 
Helpers#R()
zimbatm (author)
Tue Oct 02 07:21:48 -0700 2007
commit  4b6a2c828c5df614f2368135a41d062adc138d2a
tree    47423c90fb454506aef676ab4d9abbee5e753ca1
parent  6724a04b37902c48c44c568ec5ce53b2f1a28620
...
31
32
33
 
34
35
 
36
37
38
...
125
126
127
 
128
129
130
...
199
200
201
202
203
204
 
 
 
 
 
 
 
205
206
207
...
341
342
343
344
 
345
346
347
...
359
360
361
362
 
363
364
365
...
682
683
684
685
 
686
687
688
...
31
32
33
34
35
 
36
37
38
39
...
126
127
128
129
130
131
132
...
201
202
203
 
 
 
204
205
206
207
208
209
210
211
212
213
...
347
348
349
 
350
351
352
353
...
365
366
367
 
368
369
370
371
...
688
689
690
 
691
692
693
694
0
@@ -31,8 +31,9 @@
0
 %w[tempfile uri].map { |l| require l }
0
 
0
 class Object
0
+ # Define a method m with the passed block on the metaclass.
0
   def meta_def(m,&b)
0
- (class<<self;self end).instance_eval{define_method(m,&b)}
0
+ (class<<self;self end).send(:define_method,m,&b)
0
   end
0
 end
0
 
0
@@ -125,6 +126,7 @@ module Camping
0
         m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super
0
     end
0
     alias u merge!
0
+ undef id, type
0
   end
0
 
0
   # Helpers contains methods available in your controllers and views. You may add
0
@@ -199,9 +201,13 @@ module Camping
0
     #
0
     def R(c,*g)
0
       p,h=/\(.+?\)/,g.grep(Hash)
0
- (g-=h).inject(c.urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|
0
- s.sub p,C.escape((a[a.class.primary_key]rescue a))
0
- }+(h.any?? "?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&": "")
0
+ g-=h
0
+ raise "bad route" unless u = c.urls.find{|x|
0
+ break x if x.scan(p).size == g.size &&
0
+ /^#{x}\/?$/ =~ (x=g.inject(x){|x,a|
0
+ x.sub p,C.escape((a[a.class.primary_key]rescue a))})
0
+ }
0
+ h.any?? u+"?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&": u
0
     end
0
 
0
     # Simply builds a complete path from a path +p+ within the app. If your application is
0
@@ -341,7 +347,7 @@ module Camping
0
     #
0
     # redirect "/view/12"
0
     #
0
- def r(s, b, h = {}); @status = s; headers.merge!(h); @body = b; end
0
+ def r(s, b, h = {}); @status = s; headers.u(h); @body = b; end
0
 
0
     # Turn a controller into an array. This is designed to be used to pipe
0
     # controllers into the <tt>r</tt> method. A great way to forward your
0
@@ -359,7 +365,7 @@ module Camping
0
 
0
     def initialize(r, e, m) #:nodoc:
0
       @status, @method, @env, @headers, @root = 200, m.downcase, e,
0
- {'Content-Type'=>'text/html'}, e.SCRIPT_NAME.sub(/\/$/,'')
0
+ H['Content-Type','text/html'], e.SCRIPT_NAME.sub(/\/$/,'')
0
       @k = C.kp(e.HTTP_COOKIE)
0
       q = C.qsp(e.QUERY_STRING)
0
       @in = r
0
@@ -682,7 +688,7 @@ module Camping
0
       X.M
0
       k = X.const_get(c).new(StringIO.new,
0
              H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s)
0
- H.new(a.pop).each { |e,f| k.send("#{e}=",f) } if Hash === a[-1]
0
+ H[a.pop].each { |e,f| k.send("#{e}=",f) } if Hash === a[-1]
0
       k.service(*a)
0
     end
0
   end
...
1
2
 
3
4
5
6
7
8
9
 
 
 
 
 
 
 
 
10
11
12
13
14
15
 
16
17
18
 
 
19
20
21
...
49
50
51
52
53
 
 
54
55
...
1
 
2
3
4
 
 
 
 
 
5
6
7
8
9
10
11
12
13
14
15
16
17
 
18
19
 
 
20
21
22
23
24
...
52
53
54
 
 
55
56
57
58
0
@@ -1,21 +1,24 @@
0
 %w[tempfile uri].map{|l|require l};class Object;def meta_def m,&b
0
-(class<<self;self end).instance_eval{define_method(m,&b)}end end
0
+(class<<self;self end).send(:define_method,m,&b)end end
0
 module Camping;C=self;S=IO.read(__FILE__)rescue nil;P="Cam\ping Problem!"
0
 class H<Hash;def method_missing m,*a
0
-m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super end;alias u merge!;end
0
-module Helpers def R c,*g;p,h=/\(.+?\)/,g.grep(Hash)
0
-(g-=h).inject(c.urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|s.sub p,C.
0
-escape((a[a.class.primary_key]rescue a))}+(h.any?? "?"+h[0].map{|x|x.map{|z|C.
0
-escape z}*"="}*"&": "")end;def /(p);p[/^\//]?@root+p:p;end;def URL c='/',*a
0
+m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super end;alias u merge!
0
+undef id, type;end
0
+module Helpers def R c,*g;p,h=/\(.+?\)/,g.grep(Hash);g-=h;
0
+raise"bad route"unless u=c.urls.find{|x|break x if x.scan(p).size==g.size&&
0
+/^#{x}\/?$/=~(x=g.inject(x){|x,a|x.sub p,C.escape((a[a.class.primary_key
0
+]rescue a))})}
0
+h.any?? u+"?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&":u end
0
+def /(p);p[/^\//]?@root+p:p;end;def URL c='/',*a
0
 c=R(c,*a)if c.respond_to?:urls;c=self/c;c="//"+@env.HTTP_HOST+c if c[/^\//]
0
 URI(c) end end;module Base;attr_accessor:input,:cookies,:env,:headers,:body,
0
 :status,:root;Z="\r\n";def method_missing*a,&b;a.shift if a[0]==:render
0
 m=Mab.new({},self);s=m.capture{send(*a,&b)};s=m.capture{send(:layout){s}}if
0
 /^_/!~a[0].to_s and m.respond_to?:layout;s end;def redirect*a
0
-r 302,'','Location'=>URL(*a)end;def r s,b,h={};@status=s;headers.merge!h
0
+r 302,'','Location'=>URL(*a)end;def r s,b,h={};@status=s;headers.u h
0
 @body=b end;def to_a;[status,body,headers]end;def initialize r,e,m
0
-@status,@method,@env,@headers,@root=200,m.downcase,e,{
0
-'Content-Type'=>"text/html"},e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE
0
+@status,@method,@env,@headers,@root=200,m.downcase,e,H[
0
+'Content-Type',"text/html"],e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE
0
 q=C.qsp e.QUERY_STRING;@in=r;case e.CONTENT_TYPE
0
 when %r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n
0
 b=/(?:\r?\n|\A)#{Regexp::quote"--#$1"}(?:--)?\r$/;until
0
@@ -49,7 +52,7 @@ def kp s;c=qsp(s,';,')end;def run r=$stdin,e=ENV;X.M;e=H[e.to_hash]
0
 k,a=X.D e.PATH_INFO=un("/#{e.PATH_INFO}".gsub(/\/+/,'/'));k.new(
0
 r,e,(m=e.REQUEST_METHOD||"GET")).Y.service(*a);rescue=>x;X::ServerError.new(
0
 r,e,'get').service(k,m,x)end;def method_missing m,c,*a;X.M;k=X.const_get(c).
0
-new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s);H.
0
-new(a.pop).each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end
0
+new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s)
0
+H[a.pop].each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end
0
 module Views;include X,Helpers;end;module Models;autoload:Base,'camping/db';def
0
 Y;self;end;end;autoload:Mab,'camping/mab'end
...
25
26
27
 
 
 
28
29
30
...
37
38
39
 
40
41
42
...
54
55
56
57
 
 
58
59
60
 
 
 
 
 
 
61
62
63
64
 
65
66
67
...
25
26
27
28
29
30
31
32
33
...
40
41
42
43
44
45
46
...
58
59
60
 
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
0
@@ -25,6 +25,9 @@ module Misc
0
     class RR
0
       def get; render :rr; end
0
     end
0
+ class BadLinks
0
+ def get; render :bad_links; end
0
+ end
0
   end
0
 
0
   module Views
0
@@ -37,6 +40,7 @@ module Misc
0
             li{ a "render partial", :href=>R(RenderPartial)}
0
             li{ a "X-Sendfile", :href=>R(XSendfile)}
0
             li{ a "Links", :href=>R(Links)}
0
+ li{ a "BadLinks", :href=>R(BadLinks)}
0
             li{ a "Redirect", :href=>R(Redirect)}
0
           end
0
           p { yield }
0
@@ -54,14 +58,22 @@ module Misc
0
 
0
     def links
0
       a "plain", :href=>R(Links); br
0
- a "with hash and args", :href=>R(Links, "moo", 3, :with=>"Hash"); br
0
+ a "with args and hash", :href=>R(Links, "moo", 3, :with=>"Hash"); br
0
+ a "with args and mult. hash", :href=>R(Links, "hoi", 8, :with=>"multiple", 3=>"hash"); br
0
       # TODO : with <AR::Base object
0
     end
0
 
0
+ def bad_links
0
+ a "null controller", :href=>R(nil)
0
+ a "bad arity", :href=>R(RR, :moo)
0
+ a "bad args", :href=>R(Links, 3, "moo")
0
+ end
0
+
0
     def rr
0
       p "got redirected"
0
     end
0
 
0
+
0
   end
0
 end
0
 

Comments

    No one has commented yet.