hooks 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hooks.rb +18 -2
- data/test/hooks_test.rb +6 -0
- metadata +3 -3
data/lib/hooks.rb
CHANGED
@@ -16,7 +16,7 @@ require "hooks/inheritable_attribute"
|
|
16
16
|
#
|
17
17
|
# cat.run_hook :after_dinner
|
18
18
|
module Hooks
|
19
|
-
VERSION = "0.1.
|
19
|
+
VERSION = "0.1.2"
|
20
20
|
|
21
21
|
def self.included(base)
|
22
22
|
base.extend InheritableAttribute
|
@@ -46,13 +46,29 @@ module Hooks
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def run_hook_for(name, scope, *args)
|
49
|
-
|
49
|
+
callbacks_for_hook(name).each do |callback|
|
50
50
|
scope.send(callback, *args) and next if callback.kind_of? Symbol
|
51
51
|
callback.call(*args)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
# Returns the callbacks for +name+. Handy if you want to run the callbacks yourself, say when
|
56
|
+
# they should be executed in another context.
|
57
|
+
#
|
58
|
+
# Example:
|
59
|
+
#
|
60
|
+
# def initialize
|
61
|
+
# self.class.callbacks_for_hook(:after_eight).each do |callback|
|
62
|
+
# instance_exec(self, &callback)
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# would run callbacks in the object _instance_ context, passing +self+ as block parameter.
|
66
|
+
def callbacks_for_hook(name)
|
67
|
+
send("_#{name}_callbacks")
|
68
|
+
end
|
69
|
+
|
55
70
|
private
|
71
|
+
|
56
72
|
def define_hook_writer(hook, accessor_name)
|
57
73
|
instance_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
58
74
|
def #{hook}(method=nil, &block)
|
data/test/hooks_test.rb
CHANGED
@@ -20,6 +20,12 @@ class HooksTest < ActiveSupport::TestCase
|
|
20
20
|
@klass._after_eight_callbacks << :dine
|
21
21
|
assert_equal [:dine], @klass._after_eight_callbacks
|
22
22
|
end
|
23
|
+
|
24
|
+
should "respond to Class.callbacks_for_hook" do
|
25
|
+
assert_equal [], @klass.callbacks_for_hook(:after_eight)
|
26
|
+
@klass.after_eight :dine
|
27
|
+
assert_equal [:dine], @klass.callbacks_for_hook(:after_eight)
|
28
|
+
end
|
23
29
|
|
24
30
|
context "creates a public writer for the hook that" do
|
25
31
|
should "accepts method names" do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Nick Sutterer
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-11 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|