Skip to content

Commit

Permalink
micro-optimizations and fix for ticket #96 : more strictness for Help…
Browse files Browse the repository at this point in the history
…ers#R()
  • Loading branch information
Jonas Pfenniger committed Oct 2, 2007
1 parent 6724a04 commit 4b6a2c8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
20 changes: 13 additions & 7 deletions lib/camping-unabridged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
%w[tempfile uri].map { |l| require l }

class Object
# Define a method m with the passed block on the metaclass.
def meta_def(m,&b)
(class<<self;self end).instance_eval{define_method(m,&b)}
(class<<self;self end).send(:define_method,m,&b)
end
end

Expand Down Expand Up @@ -125,6 +126,7 @@ def method_missing(m,*a)
m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super
end
alias u merge!
undef id, type
end

# Helpers contains methods available in your controllers and views. You may add
Expand Down Expand Up @@ -199,9 +201,13 @@ module Helpers
#
def R(c,*g)
p,h=/\(.+?\)/,g.grep(Hash)
(g-=h).inject(c.urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|
s.sub p,C.escape((a[a.class.primary_key]rescue a))
}+(h.any?? "?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&": "")
g-=h
raise "bad route" unless u = c.urls.find{|x|
break x if x.scan(p).size == g.size &&
/^#{x}\/?$/ =~ (x=g.inject(x){|x,a|
x.sub p,C.escape((a[a.class.primary_key]rescue a))})
}
h.any?? u+"?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&": u
end

# Simply builds a complete path from a path +p+ within the app. If your application is
Expand Down Expand Up @@ -341,7 +347,7 @@ def redirect(*a)
#
# redirect "/view/12"
#
def r(s, b, h = {}); @status = s; headers.merge!(h); @body = b; end
def r(s, b, h = {}); @status = s; headers.u(h); @body = b; end

# Turn a controller into an array. This is designed to be used to pipe
# controllers into the <tt>r</tt> method. A great way to forward your
Expand All @@ -359,7 +365,7 @@ def to_a;[status, body, headers] end

def initialize(r, e, m) #:nodoc:
@status, @method, @env, @headers, @root = 200, m.downcase, e,
{'Content-Type'=>'text/html'}, e.SCRIPT_NAME.sub(/\/$/,'')
H['Content-Type','text/html'], e.SCRIPT_NAME.sub(/\/$/,'')
@k = C.kp(e.HTTP_COOKIE)
q = C.qsp(e.QUERY_STRING)
@in = r
Expand Down Expand Up @@ -682,7 +688,7 @@ def method_missing(m, c, *a)
X.M
k = X.const_get(c).new(StringIO.new,
H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s)
H.new(a.pop).each { |e,f| k.send("#{e}=",f) } if Hash === a[-1]
H[a.pop].each { |e,f| k.send("#{e}=",f) } if Hash === a[-1]
k.service(*a)
end
end
Expand Down
25 changes: 14 additions & 11 deletions lib/camping.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
%w[tempfile uri].map{|l|require l};class Object;def meta_def m,&b
(class<<self;self end).instance_eval{define_method(m,&b)}end end
(class<<self;self end).send(:define_method,m,&b)end end
module Camping;C=self;S=IO.read(__FILE__)rescue nil;P="Cam\ping Problem!"
class H<Hash;def method_missing m,*a
m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super end;alias u merge!;end
module Helpers def R c,*g;p,h=/\(.+?\)/,g.grep(Hash)
(g-=h).inject(c.urls.find{|x|x.scan(p).size==g.size}.dup){|s,a|s.sub p,C.
escape((a[a.class.primary_key]rescue a))}+(h.any?? "?"+h[0].map{|x|x.map{|z|C.
escape z}*"="}*"&": "")end;def /(p);p[/^\//]?@root+p:p;end;def URL c='/',*a
m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super end;alias u merge!
undef id, type;end
module Helpers def R c,*g;p,h=/\(.+?\)/,g.grep(Hash);g-=h;
raise"bad route"unless u=c.urls.find{|x|break x if x.scan(p).size==g.size&&
/^#{x}\/?$/=~(x=g.inject(x){|x,a|x.sub p,C.escape((a[a.class.primary_key
]rescue a))})}
h.any?? u+"?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&":u end
def /(p);p[/^\//]?@root+p:p;end;def URL c='/',*a
c=R(c,*a)if c.respond_to?:urls;c=self/c;c="//"+@env.HTTP_HOST+c if c[/^\//]
URI(c) end end;module Base;attr_accessor:input,:cookies,:env,:headers,:body,
:status,:root;Z="\r\n";def method_missing*a,&b;a.shift if a[0]==:render
m=Mab.new({},self);s=m.capture{send(*a,&b)};s=m.capture{send(:layout){s}}if
/^_/!~a[0].to_s and m.respond_to?:layout;s end;def redirect*a
r 302,'','Location'=>URL(*a)end;def r s,b,h={};@status=s;headers.merge!h
r 302,'','Location'=>URL(*a)end;def r s,b,h={};@status=s;headers.u h
@body=b end;def to_a;[status,body,headers]end;def initialize r,e,m
@status,@method,@env,@headers,@root=200,m.downcase,e,{
'Content-Type'=>"text/html"},e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE
@status,@method,@env,@headers,@root=200,m.downcase,e,H[
'Content-Type',"text/html"],e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE
q=C.qsp e.QUERY_STRING;@in=r;case e.CONTENT_TYPE
when %r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n
b=/(?:\r?\n|\A)#{Regexp::quote"--#$1"}(?:--)?\r$/;until
Expand Down Expand Up @@ -49,7 +52,7 @@ def kp s;c=qsp(s,';,')end;def run r=$stdin,e=ENV;X.M;e=H[e.to_hash]
k,a=X.D e.PATH_INFO=un("/#{e.PATH_INFO}".gsub(/\/+/,'/'));k.new(
r,e,(m=e.REQUEST_METHOD||"GET")).Y.service(*a);rescue=>x;X::ServerError.new(
r,e,'get').service(k,m,x)end;def method_missing m,c,*a;X.M;k=X.const_get(c).
new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s);H.
new(a.pop).each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end
new(StringIO.new,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s)
H[a.pop].each{|e,f|k.send("#{e}=",f)}if Hash===a[-1];k.service(*a);end;end
module Views;include X,Helpers;end;module Models;autoload:Base,'camping/db';def
Y;self;end;end;autoload:Mab,'camping/mab'end
14 changes: 13 additions & 1 deletion test/apps/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def get; redirect(RR) end
class RR
def get; render :rr; end
end
class BadLinks
def get; render :bad_links; end
end
end

module Views
Expand All @@ -37,6 +40,7 @@ def layout
li{ a "render partial", :href=>R(RenderPartial)}
li{ a "X-Sendfile", :href=>R(XSendfile)}
li{ a "Links", :href=>R(Links)}
li{ a "BadLinks", :href=>R(BadLinks)}
li{ a "Redirect", :href=>R(Redirect)}
end
p { yield }
Expand All @@ -54,14 +58,22 @@ def index

def links
a "plain", :href=>R(Links); br
a "with hash and args", :href=>R(Links, "moo", 3, :with=>"Hash"); br
a "with args and hash", :href=>R(Links, "moo", 3, :with=>"Hash"); br
a "with args and mult. hash", :href=>R(Links, "hoi", 8, :with=>"multiple", 3=>"hash"); br
# TODO : with <AR::Base object
end

def bad_links
a "null controller", :href=>R(nil)
a "bad arity", :href=>R(RR, :moo)
a "bad args", :href=>R(Links, 3, "moo")
end

def rr
p "got redirected"
end


end
end

Expand Down

0 comments on commit 4b6a2c8

Please sign in to comment.