Skip to content

Commit

Permalink
* lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
Browse files Browse the repository at this point in the history
….)

 * lib/camping-unabridged.rb: ditto and some small alterations to get camping-unabridged.rb's self-eval working again.
 * examples/tepee.rb: changes its home link to its new location in the repo.
  • Loading branch information
_why committed Jul 6, 2006
1 parent 1109984 commit 8771109
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -6,7 +6,7 @@ require 'fileutils'
include FileUtils

NAME = "camping"
VERS = "1.4.115"
VERS = "1.4.120"
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
RDOC_OPTS = ['--quiet', '--title', "Camping, the Documentation",
"--template", "extras/flipbook_rdoc.rb",
Expand Down Expand Up @@ -50,7 +50,7 @@ spec =
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)\/', '--exclude', 'lib/camping.rb']
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)\/', '--exclude', 'lib/camping(-orig)?.rb']
s.summary = "minature rails for stay-at-home moms"
s.description = s.summary
s.author = "why the lucky stiff"
Expand Down
2 changes: 1 addition & 1 deletion examples/tepee.rb
Expand Up @@ -70,7 +70,7 @@ def layout
body do
p do
small do
span "welcome to " ; a 'tepee', :href => "http://code.whytheluckystiff.net/svn/camping/trunk/examples/tepee/"
span "welcome to " ; a 'tepee', :href => "http://code.whytheluckystiff.net/svn/camping/trunk/examples/tepee.rb"
span '. go ' ; a 'home', :href => R(Show, 'home_page')
span '. list all ' ; a 'pages', :href => R(List)
end
Expand Down
104 changes: 61 additions & 43 deletions lib/camping-unabridged.rb
Expand Up @@ -95,7 +95,7 @@ module Camping
#
Apps = []
C = self
S = IO.read(__FILE__).sub(/S=I.+$/,'')
S = IO.read(__FILE__).sub(/^ S = I.+$/,'')
P="Cam\ping Problem!"

H = HashWithIndifferentAccess
Expand Down Expand Up @@ -438,6 +438,12 @@ def markaby #:nodoc:
Mab.new( instance_variables.map { |iv|
[iv[1..-1], instance_variable_get(iv)] } )
end
def markaview m,*a,&b #:nodoc:
h=markaby
h.send m,*a,&b
h.to_s
end
end
# Controllers is a module for placing classes which handle URLs. This is done
Expand All @@ -460,6 +466,41 @@ def markaby #:nodoc:
# NotFound class handles URLs not found. The ServerError class handles exceptions
# uncaught by your application.
module Controllers
class << self
# Add routes to a controller class by piling them into the R method.
#
# module Camping::Controllers
# class Edit < R '/edit/(\d+)', '/new'
# def get(id)
# if id # edit
# else # new
# end
# end
# end
# end
#
# You will need to use routes in either of these cases:
#
# * You want to assign multiple routes to a controller.
# * You want your controller to receive arguments.
#
# Most of the time the rules inferred by dispatch method Controllers::D will get you
# by just fine.
def R(*urls); Class.new { meta_def(:urls) { urls } }; end
# Dispatch routes to controller classes. Classes are searched in no particular order.
# For each class, routes are checked for a match based on their order in the routing list
# given to Controllers::R. If no routes were given, the dispatcher uses a slash followed
# by the name of the controller lowercased.
def D(path)
constants.inject(nil) do |d,c|
k = const_get(c)
k.meta_def(:urls){["/#{c.downcase}"]}if !k.respond_to? :urls
d||([k, $~[1..-1]] if k.urls.find { |x| path =~ /^#{x}\/?$/ })
end||[NotFound, [path]]
end
end
# The NotFound class is a special controller class for handling 404 errors, in case you'd
# like to alter the appearance of the 404. The path is passed in as +p+.
#
Expand All @@ -475,7 +516,11 @@ module Controllers
# end
# end
#
class NotFound; def get(p); r(404, div{h1(P);h2("#{p} not found")}); end end
class NotFound < R()
def get(p)
r(404, Mab.new{h1(P);h2("#{p} not found")})
end
end
# The ServerError class is a special controller class for handling many (but not all) 500 errors.
# If there is a parse error in Camping or in your application's source code, it will not be caught
Expand All @@ -500,40 +545,14 @@ class NotFound; def get(p); r(404, div{h1(P);h2("#{p} not found")}); end end
# end
# end
#
class ServerError; include Base; def get(k,m,e); r(500, Mab.new { h1(P); h2 "#{k}.#{m}"; h3 "#{e.class} #{e.message}:"; ul { e.backtrace.each { |bt| li bt } } }.to_s) end end
class << self
# Add routes to a controller class by piling them into the R method.
#
# module Camping::Controllers
# class Edit < R '/edit/(\d+)', '/new'
# def get(id)
# if id # edit
# else # new
# end
# end
# end
# end
#
# You will need to use routes in either of these cases:
#
# * You want to assign multiple routes to a controller.
# * You want your controller to receive arguments.
#
# Most of the time the rules inferred by dispatch method Controllers::D will get you
# by just fine.
def R(*urls); Class.new { meta_def(:urls) { urls } }; end
# Dispatch routes to controller classes. Classes are searched in no particular order.
# For each class, routes are checked for a match based on their order in the routing list
# given to Controllers::R. If no routes were given, the dispatcher uses a slash followed
# by the name of the controller lowercased.
def D(path)
constants.inject(nil) do |d,c|
k = const_get(c)
k.meta_def(:urls){["/#{c.downcase}"]}if !k.respond_to? :urls
d||([k, $~[1..-1]] if k.urls.find { |x| path =~ /^#{x}\/?$/ })
end||[NotFound, [path]]
class ServerError < R()
def get(k,m,e)
r(500, Mab.new {
h1(P)
h2 "#{k}.#{m}"
h3 "#{e.class} #{e.message}:"
ul { e.backtrace.each { |bt| li bt } }
}.to_s)
end
end
end
Expand All @@ -560,14 +579,14 @@ def goes(m)
# Camping.escape("I'd go to the museum straightway!")
# #=> "I%27d+go+to+the+museum+straightway%21"
#
def escape(s); s.to_s.gsub(/[^ \w.-]+/n){'%'+($&.unpack('H2'*$&.size)*'%').upcase}.tr(' ', '+') end
# Unescapes a URL-encoded string.
#
# Camping.un("I%27d+go+to+the+museum+straightway%21")
# #=> "I'd go to the museum straightway!"
#
def un(s); s.tr('+', ' ').gsub(/%([\da-f]){2}/in){[$1].pack('H*')} end
def un(s); s.tr('+', ' ').gsub(/%([\da-f]{2})/in){[$1].pack('H*')} end
# Parses a query string into an Camping::H object.
#
Expand Down Expand Up @@ -632,20 +651,19 @@ def method_missing(m, c, *a)
#
def run(r=$stdin,e=ENV)
k, a = Controllers.D un("/#{e['PATH_INFO']}".gsub(%r!/+!,'/'))
i k
k.new(r,e,(m=e['REQUEST_METHOD']||"GET")).service(*a)
i(k).new(r,e,(m=e['REQUEST_METHOD']||"GET")).service(*a)
rescue Exception => x
Controllers::ServerError.new(r,e,'get').service(k,m,x)
i(Controllers::ServerError).new(r,e,'get').service(k,m,x)
end
def method_missing(m, c, *a)
k = Controllers.const_get(c)
i k
k.new(nil,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s).service(*a)
i(k).new(nil,H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s).service(*a)
end
def i k
k.send(:include, C, Base, Models) if !(k<C)
k
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/camping.rb
Expand Up @@ -27,25 +27,25 @@ module Camping;Apps=[];C=self;S=IO.read(__FILE__).sub(/S=I.+$/,'')
|k,v|[*v].map{|x|"#{k}: #{x}"}}*"\r\n"}\r\n\r\n#{@body}" end;def markaby;Mab.new(
instance_variables.map{|iv|[iv[1..-1],instance_variable_get(iv)]}) end;def
markaview m,*a,&b;h=markaby;h.send m,*a,&b;h.to_s end end
X=module Controllers;class NotFound;def get p;r(404,div{h1 P
h2 p+" not found"}) end end;class ServerError;include Base;def get k,m,e;r(500,
Mab.new{h1 P;h2 "#{k}.#{m}";h3 "#{e.class} #{e.message}:";ul{
e.backtrace.each{|bt|li(bt)}}}.to_s)end end;class<<self;def R *urls;Class.new{
X=module Controllers;class<<self;def R *urls;Class.new{
meta_def(:urls){urls}}end;def D path;constants.inject(nil){|d,c|k=const_get c
k.meta_def(:urls){["/#{c.downcase}"]}if !k.respond_to? :urls;d||([k,$~[1..-1]]if k.urls.find{
|x|path=~/^#{x}\/?$/})}||[NotFound,[path]] end end;self;end;class<<self;def goes m
|x|path=~/^#{x}\/?$/})}||[NotFound,[path]] end end;class NotFound<R();def get p;r(404,Mab.new{h1 P
h2 p+" not found"}) end end;class ServerError<R();def get k,m,e;r(500,
Mab.new{h1 P;h2 "#{k}.#{m}";h3 "#{e.class} #{e.message}:";ul{
e.backtrace.each{|bt|li(bt)}}}.to_s)end end;self;end;class<<self;def goes m
eval(S.gsub(/Camping/,m.to_s),TOPLEVEL_BINDING);Apps<<const_get(m);end
def escape(s)s.to_s.gsub(/[^ \w.-]+/n){'%'+($&.unpack('H2'*$&.size)*'%').upcase}.tr(' ','+')end
def un(s)s.tr('+',' ').gsub(/%([\da-f]{2})/in){[$1].pack('H*')} end
def qs_parse q,d='&;';m=proc{|_,o,n|o.u(n,&m)rescue([*o
]<<n)};q.to_s.split(/[#{d}] */n).inject(H[]){|h,p|k,v=un(p).split('=',2)
h.u k.split(/[\]\[]+/).reverse.inject(v){|x,i|H[i,x]},&m}end;def kp(s);c=
qs_parse(s,';,')end;def run r=$stdin,e=ENV;k,a=X.D un("/#{e['PATH_INFO']
}".gsub(/\/+/,'/'));i k;k.new(r,e,(m=e['REQUEST_METHOD'
]||"GET")).service *a;rescue Exception=>x;X::ServerError.new(r,e,'get').service(
k,m,x)end;def method_missing m,c,*a;k=X.const_get c;i k;k.new('',
}".gsub(/\/+/,'/'));i(k).new(r,e,(m=e['REQUEST_METHOD'
]||"GET")).service *a;rescue Exception=>x;i(X::ServerError).new(r,e,'get').service(
k,m,x)end;def method_missing m,c,*a;k=X.const_get c;i(k).new('',
H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s).service *a;end
def i k;k.send(:include,C,Base,Models)if !(k<C) end
def i k;k.send(:include,C,Base,Models)if !(k<C);k end
end;module Views;include X,Helpers end;module Models;A=
ActiveRecord;Base=A::Base;def Base.table_name_prefix;"#{name[/\w+/]}_".
downcase.sub(/^(#{A}|camping)_/i,'')end end;class Mab<Markaby::Builder;include \
Expand Down

0 comments on commit 8771109

Please sign in to comment.