lib/roda/plugins/hooks.rb in roda-3.10.0 vs lib/roda/plugins/hooks.rb in roda-3.11.0
- old
+ new
@@ -28,12 +28,18 @@
# handle cases where before hooks are added after the route block.
#
# Note that the after hook is called with the rack response array
# of status, headers, and body. If it wants to change the response,
# it must mutate this argument, calling <tt>response.status=</tt> inside
- # an after block will not affect the returned status.
+ # an after block will not affect the returned status. Note that after
+ # hooks can be called with nil if an exception is raised during routing.
module Hooks
+ def self.load_dependencies(app)
+ app.plugin :_before_hook
+ app.plugin :_after_hook
+ end
+
def self.configure(app)
app.opts[:before_hook] ||= nil
app.opts[:after_hook] ||= nil
end
@@ -68,22 +74,22 @@
end
end
end
module InstanceMethods
- # Before routing, execute the before hooks, and
- # execute the after hooks before returning.
- def call(&block)
- res = super do |r|
- if b = opts[:before_hook]
- instance_exec(&b)
- end
+ private
- instance_exec(r, &block)
- end
- ensure
+ # Run after hooks.
+ def _roda_after_90(res)
if b = opts[:after_hook]
instance_exec(res, &b)
+ end
+ end
+
+ # Run before hooks.
+ def _roda_before_10
+ if b = opts[:before_hook]
+ instance_exec(&b)
end
end
end
end