lib/jellyfish.rb in jellyfish-0.8.0 vs lib/jellyfish.rb in jellyfish-0.9.0
- old
+ new
@@ -9,10 +9,11 @@
autoload :NormalizedPath , 'jellyfish/normalized_path'
autoload :ChunkedBody, 'jellyfish/chunked_body'
GetValue = Object.new
+ Identity = lambda{|_|_}
class Response < RuntimeError
def headers
@headers ||= {'Content-Type' => 'text/html'}
end
@@ -55,12 +56,12 @@
raise
end
def request ; @request ||= Rack::Request.new(env); end
def halt *args; throw(:halt, *args) ; end
- def forward ; raise(Jellyfish::NotFound.new) ; end
- def found url; raise(Jellyfish:: Found.new(url)); end
+ def forward ; halt(Jellyfish::NotFound.new) ; end
+ def found url; halt(Jellyfish:: Found.new(url)); end
alias_method :redirect, :found
def path_info ; env['PATH_INFO'] || '/' ; end
def request_method; env['REQUEST_METHOD'] || 'GET'; end
@@ -94,11 +95,11 @@
end
end
private
def actions
- routes[request_method.downcase] || raise(Jellyfish::NotFound.new)
+ routes[request_method.downcase] || halt(Jellyfish::NotFound.new)
end
def dispatch
actions.find{ |(route, block)|
case route
@@ -106,11 +107,11 @@
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(Jellyfish::NotFound.new)
+ } || halt(Jellyfish::NotFound.new)
end
def with_each value
if value.respond_to?(:each) then value else [value] end
end
@@ -172,20 +173,17 @@
def initialize app=nil; @app = app; end
def call env
ctrl = self.class.controller.new(self.class.routes, self)
- ctrl.call(env)
- rescue NotFound => e # forward
- if app
- begin
- app.call(env)
- rescue Exception => e
- handle(ctrl, e, env['rack.errors'])
- end
+ case res = catch(:halt){ ctrl.call(env) }
+ when NotFound
+ app && forward(ctrl, env) || handle(ctrl, res)
+ when Response
+ handle(ctrl, res, env['rack.errors'])
else
- handle(ctrl, e)
+ res || ctrl.block_call(nil, Identity)
end
rescue Exception => e
handle(ctrl, e, env['rack.errors'])
end
@@ -198,9 +196,15 @@
return unless stderr
stderr.puts("[#{self.class.name}] #{msg}")
end
private
+ def forward ctrl, env
+ app.call(env)
+ rescue Exception => e
+ handle(ctrl, e, env['rack.errors'])
+ end
+
def handle ctrl, e, stderr=nil
if handler = best_handler(e)
ctrl.block_call(e, handler)
elsif !self.class.handle_exceptions
raise e