lib/jellyfish.rb in jellyfish-0.3.0 vs lib/jellyfish.rb in jellyfish-0.4.0

- old
+ new

@@ -1,8 +1,9 @@ module Jellyfish autoload :VERSION, 'jellyfish/version' + autoload :Sinatra, 'jellyfish/sinatra' REQUEST_METHOD = 'REQUEST_METHOD' PATH_INFO = 'PATH_INFO' LOCATION = 'Location' RACK_ERRORS = 'rack.errors' @@ -17,12 +18,12 @@ def body @body ||= [File.read("#{Jellyfish.public_root}/#{status}.html")] end end - class NotFound < Respond; def status; 404; end; end class InternalError < Respond; def status; 500; end; end + class NotFound < Respond; def status; 404; end; end class Found < Respond attr_reader :url def initialize url; @url = url ; end def status ; 302 ; end def headers ; super.merge(LOCATION => url) ; end @@ -92,15 +93,15 @@ end def dispatch actions.find{ |(route, block)| case route - when Regexp - match = route.match(path_info) - break match, block if match when String break route, block if route == path_info + else#Regexp, using else allows you to use custom matcher + match = route.match(path_info) + break match, block if match end } || raise(NotFound.new) end end @@ -130,42 +131,43 @@ end # ----------------------------------------------------------------- def initialize app=nil; @app = app; end + def controller ; Controller; end def call env - controller = Controller.new(self.class.routes) - controller.call(env) + ctrl = controller.new(self.class.routes) + ctrl.call(env) rescue NotFound => e # forward if app - protect(controller, env){ app.call(env) } + protect(ctrl, env){ app.call(env) } else - handle(controller, e) + handle(ctrl, e) end rescue Exception => e - handle(controller, e, env[RACK_ERRORS]) + handle(ctrl, e, env[RACK_ERRORS]) end - def protect controller, env + def protect ctrl, env yield rescue Exception => e - handle(controller, e, env[RACK_ERRORS]) + handle(ctrl, e, env[RACK_ERRORS]) end private - def handle controller, e, stderr=nil + def handle ctrl, e, stderr=nil raise e unless self.class.handle_exceptions handler = self.class.handlers.find{ |klass, block| break block if e.kind_of?(klass) } if handler - controller.block_call(e, handler) + ctrl.block_call(e, handler) elsif e.kind_of?(Respond) # InternalError ends up here if no handlers [e.status, e.headers, e.body] else # fallback and see if there's any InternalError handler log_error(e, stderr) - handle(controller, InternalError.new) + handle(ctrl, InternalError.new) end end def log_error e, stderr return unless stderr