summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2011-09-13 10:12:55 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2011-09-13 10:12:55 +0200
commit285fa24544bf5feffb08b56deb03ec224f1726b4 (patch)
tree8b355476082aece51b41bd9516e18aa861488fbf
parentb06ae981c92040fe06c0317f0d12dc3f02044d21 (diff)
downloadzorglub-285fa24544bf5feffb08b56deb03ec224f1726b4.zip
zorglub-285fa24544bf5feffb08b56deb03ec224f1726b4.tar.gz
add before_all and after_all hooks
-rw-r--r--examples/layout/default.haml1
-rw-r--r--examples/sample.ru10
-rw-r--r--lib/zorglub/node.rb27
3 files changed, 38 insertions, 0 deletions
diff --git a/examples/layout/default.haml b/examples/layout/default.haml
index 6af5e38..8df684f 100644
--- a/examples/layout/default.haml
+++ b/examples/layout/default.haml
@@ -1,4 +1,5 @@
%h1=@title
%p="xx <b>default</b> layout xx"
+%p="count #{Zorglub::Node.count}"
%p=@content
%p="xx -- xx"
diff --git a/examples/sample.ru b/examples/sample.ru
index 67937ac..6784d3d 100644
--- a/examples/sample.ru
+++ b/examples/sample.ru
@@ -13,6 +13,16 @@ Zorglub::Config.engine = 'haml'
Zorglub::Config.session_on = true
Zorglub::Config.root = File.dirname( File.absolute_path(__FILE__) )
#
+class Zorglub::Node
+ @count=0
+ class << self
+ attr_accessor :count
+ end
+ before_all do |node|
+ Zorglub::Node.count +=1
+ end
+end
+#
class Node1 < Zorglub::Node
#
def index a1, *a2
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
@@ -4,8 +4,15 @@ module Zorglub
#
class Node
#
+ @hooks = {
+ :before_all => [],
+ :after_all => [],
+ }
+ #
class << self
#
+ attr_reader :hooks
+ #
def engine engine=nil
@engine = engine unless engine.nil?
@engine ||= Config.engine
@@ -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
#