hooks 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +4 -0
- data/lib/hooks.rb +8 -1
- data/lib/hooks/instance_hooks.rb +14 -2
- data/lib/hooks/version.rb +1 -1
- data/test/instance_hooks_test.rb +15 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b837493d808a39dba9923d6f57b6e679df91c1c
|
4
|
+
data.tar.gz: 5ab4d80c78c0fa39946fbbfb50e255d12e4a0cdb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23344cb417f370797f6de7cdcc4974bba3cd958b9a4d0c90ae4a639197b9f163081819498d8349a3851376f0c1c127e42e79133b1e6e9b5d9d3128d2f872136e
|
7
|
+
data.tar.gz: 9dacc24205cd2ecabc682c5fe5654d8119b2d4a123fe8095753696ddcf168817d7e578b34abe68f0a5759067f5dfe7f3f9ced4fb671ff4da9788a6f71eff7aa4
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## 0.3.3
|
2
|
+
|
3
|
+
* Fix a bug where the hook writer method (e.g. `#after_dark`) wasn't available on the instance even when `InstanceHooks` was included.
|
4
|
+
|
1
5
|
## 0.3.2
|
2
6
|
|
3
7
|
* Added `Hooks::InstanceHooks` to add hooks and/or callbacks on instance level. Thanks to @mpapis for that suggestion.
|
data/lib/hooks.rb
CHANGED
@@ -77,11 +77,18 @@ module Hooks
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def define_hook_writer(name)
|
80
|
-
instance_eval
|
80
|
+
instance_eval *hook_writer_args(name)
|
81
|
+
end
|
82
|
+
|
83
|
+
def hook_writer_args(name)
|
84
|
+
# DISCUSS: isn't there a simpler way to define a dynamic method? should the internal logic be handled by HooksSet instead?
|
85
|
+
str = <<-RUBY_EVAL
|
81
86
|
def #{name}(method=nil, &block)
|
82
87
|
_hooks[:#{name}] << (block || method)
|
83
88
|
end
|
84
89
|
RUBY_EVAL
|
90
|
+
|
91
|
+
[str, __FILE__, __LINE__ + 1]
|
85
92
|
end
|
86
93
|
|
87
94
|
def extract_options!(args)
|
data/lib/hooks/instance_hooks.rb
CHANGED
@@ -2,12 +2,24 @@ module Hooks
|
|
2
2
|
module InstanceHooks
|
3
3
|
include ClassMethods
|
4
4
|
|
5
|
+
def run_hook(name, *args)
|
6
|
+
run_hook_for(name, self, *args)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
5
10
|
def _hooks
|
6
11
|
@_hooks ||= self.class._hooks.clone # TODO: generify that with representable_attrs.
|
7
12
|
end
|
8
13
|
|
9
|
-
|
10
|
-
|
14
|
+
module ClassMethods
|
15
|
+
def define_hook_writer(name)
|
16
|
+
super
|
17
|
+
class_eval *hook_writer_args(name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.included(base)
|
22
|
+
base.extend(ClassMethods)
|
11
23
|
end
|
12
24
|
end
|
13
25
|
end
|
data/lib/hooks/version.rb
CHANGED
data/test/instance_hooks_test.rb
CHANGED
@@ -6,7 +6,11 @@ class InstanceHooksTest < HooksTest
|
|
6
6
|
include Hooks::InstanceHooks
|
7
7
|
end }
|
8
8
|
|
9
|
-
subject { klass.new
|
9
|
+
subject { klass.new.tap do |obj|
|
10
|
+
obj.instance_eval do
|
11
|
+
def dine; executed << :dine; end
|
12
|
+
end
|
13
|
+
end }
|
10
14
|
|
11
15
|
it "adds hook to instance" do
|
12
16
|
subject.define_hook :after_eight
|
@@ -32,9 +36,16 @@ class InstanceHooksTest < HooksTest
|
|
32
36
|
end
|
33
37
|
|
34
38
|
it "responds to #run_hook" do
|
35
|
-
subject.
|
36
|
-
|
37
|
-
|
39
|
+
subject.run_hook :after_eight
|
40
|
+
subject.executed.must_equal [:dine]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#after_eight from class (no define_hook in instance)" do
|
45
|
+
it "responds to #after_eight" do
|
46
|
+
klass.define_hook :after_eight
|
47
|
+
|
48
|
+
subject.after_eight :dine
|
38
49
|
|
39
50
|
subject.run_hook :after_eight
|
40
51
|
subject.executed.must_equal [:dine]
|