From 285fa24544bf5feffb08b56deb03ec224f1726b4 Mon Sep 17 00:00:00 2001 From: Jérémy Zurcher Date: Tue, 13 Sep 2011 10:12:55 +0200 Subject: add before_all and after_all hooks --- lib/zorglub/node.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lib') diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb index 6e0989f..e80c051 100644 --- a/lib/zorglub/node.rb +++ b/lib/zorglub/node.rb @@ -3,8 +3,15 @@ module Zorglub # class Node + # + @hooks = { + :before_all => [], + :after_all => [], + } # class << self + # + attr_reader :hooks # def engine engine=nil @engine = engine unless engine.nil? @@ -42,6 +49,24 @@ module Zorglub node.content end # + def before_hooks obj + Node.hooks[:before_all].each do |blk| blk.call obj end + end + # + def before_all &blk + Node.hooks[:before_all]<< blk + Node.hooks[:before_all].uniq! + end + # + def after_hooks obj + Node.hooks[:after_all].each do |blk| blk.call obj end + end + # + def after_all &blk + Node.hooks[:after_all]<< blk + Node.hooks[:after_all].uniq! + end + # def error_404 node resp = node.response resp.status = 404 @@ -71,11 +96,13 @@ module Zorglub end # def feed! + Node.before_hooks self @content = self.send @action[:method], *@action[:args] e, v, l = Config.engine_proc(@action[:engine]), view, layout # TODO compile and cache @content = e.call v, self if e and File.exists? v @content = e.call l, self if e and File.exists? l + Node.after_hooks self @content end # -- cgit v1.2.3