From 8771109cf7e665ea43aa0eba43c6b1f3f6f9b032 Mon Sep 17 00:00:00 2001 From: _why Date: Thu, 6 Jul 2006 18:02:00 +0000 Subject: [PATCH] * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67.) * 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. --- Rakefile | 4 +- examples/tepee.rb | 2 +- lib/camping-unabridged.rb | 104 ++++++++++++++++++++++---------------- lib/camping.rb | 18 +++---- 4 files changed, 73 insertions(+), 55 deletions(-) diff --git a/Rakefile b/Rakefile index b5d6ca4..ad42449 100644 --- a/Rakefile +++ b/Rakefile @@ -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", @@ -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" diff --git a/examples/tepee.rb b/examples/tepee.rb index 2920438..ce7faad 100755 --- a/examples/tepee.rb +++ b/examples/tepee.rb @@ -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 diff --git a/lib/camping-unabridged.rb b/lib/camping-unabridged.rb index 6a7b714..709aba8 100644 --- a/lib/camping-unabridged.rb +++ b/lib/camping-unabridged.rb @@ -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 @@ -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 @@ -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+. # @@ -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 @@ -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 @@ -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. # @@ -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 !(kx;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