hooks 0.4.0 → 0.4.1
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/.travis.yml +1 -1
- data/CHANGES.md +4 -0
- data/README.md +21 -1
- data/hooks.gemspec +3 -4
- data/lib/hooks.rb +6 -2
- data/lib/hooks/hook.rb +6 -0
- data/lib/hooks/version.rb +1 -1
- data/test/hook_test.rb +7 -1
- data/test/hooks_test.rb +21 -5
- metadata +9 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fad249f87b5002ed27f3174c9268cad5f00be86
|
4
|
+
data.tar.gz: 5beade083dcdc44fb292bfef06018e9fec522a50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c883e89e33556a57c0df0e375d0be763edad476b7a9b0ef013397c1a7990d36d7d82a50549944d9818778969745e6d069da8af7ab1661f2c6b5e2e343f3cf4ef
|
7
|
+
data.tar.gz: f61d4bddc22992f25d705332bb8ad729f2c32d5ba3fd46af901501510b53b7a85a40fe02c588c66d36753cdabb7506bcea29ce393f5d3e6562c0fef3b797955c
|
data/.travis.yml
CHANGED
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -138,6 +138,26 @@ This will only run the first two callbacks. Note that the result doesn't contain
|
|
138
138
|
result.halted? #=> true
|
139
139
|
```
|
140
140
|
|
141
|
+
## Execution Scope
|
142
|
+
|
143
|
+
Normally, callbacks are executed in object context. You're free to provide your own context object using `:scope`.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
define_hook :before_lunch, scope: lambda { |callback, scope| Logger }
|
147
|
+
```
|
148
|
+
|
149
|
+
This will evaluate the lambda in `Logger` class context.
|
150
|
+
|
151
|
+
Return `nil` to execute the lambda in the original context.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
define_hook :before_lunch, scope: lambda { |*| nil }
|
155
|
+
what = "hands"
|
156
|
+
before_lunch << lambda { puts "wash #{what}" } # executed in original context.
|
157
|
+
```
|
158
|
+
|
159
|
+
The `:scope` lambda is executed for every added callback per run, hence the block options.
|
160
|
+
|
141
161
|
## Instance Hooks
|
142
162
|
|
143
163
|
You can also define hooks and/or add callbacks per instance. This is helpful if your class should define a basic set of hooks and callbacks that are then extended by instances.
|
@@ -197,6 +217,6 @@ gem "hooks"
|
|
197
217
|
|
198
218
|
## License
|
199
219
|
|
200
|
-
Copyright (c) 2013, Nick Sutterer
|
220
|
+
Copyright (c) 2013-2015, Nick Sutterer
|
201
221
|
|
202
222
|
Released under the MIT License.
|
data/hooks.gemspec
CHANGED
@@ -12,15 +12,14 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.homepage = "http://nicksda.apotomo.de/2010/09/hooks-and-callbacks-for-ruby-but-simple/"
|
13
13
|
s.summary = %q{Generic hooks with callbacks for Ruby.}
|
14
14
|
s.description = %q{Declaratively define hooks, add callbacks and run them with the options you like.}
|
15
|
-
s.license
|
15
|
+
s.license = "MIT"
|
16
16
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.require_paths = ["lib"]
|
20
|
-
s.license = 'MIT'
|
21
20
|
|
22
|
-
s.add_dependency "uber", "~> 0.0.
|
21
|
+
s.add_dependency "uber", "~> 0.0.14"
|
23
22
|
|
24
|
-
s.add_development_dependency "minitest", ">= 5.
|
23
|
+
s.add_development_dependency "minitest", ">= 5.4.1"
|
25
24
|
s.add_development_dependency "rake"
|
26
25
|
end
|
data/lib/hooks.rb
CHANGED
@@ -58,14 +58,18 @@ module Hooks
|
|
58
58
|
# Returns the callbacks for +name+. Handy if you want to run the callbacks yourself, say when
|
59
59
|
# they should be executed in another context.
|
60
60
|
#
|
61
|
+
# As callbacks can be static values, lambdas or methods, they're wrapped by Uber::Option::Value
|
62
|
+
# which gives you a very convenient way to execute the callback without knowing its type using
|
63
|
+
# <tt>Value#evaluate</tt>.
|
64
|
+
#
|
61
65
|
# Example:
|
62
66
|
#
|
63
67
|
# def initialize
|
64
68
|
# self.class.callbacks_for_hook(:after_eight).each do |callback|
|
65
|
-
#
|
69
|
+
# callback.evaluate(self, "create")
|
66
70
|
# end
|
67
71
|
#
|
68
|
-
#
|
72
|
+
# Runs callbacks in the object _instance_ context and pass "create" as the only argument.
|
69
73
|
def callbacks_for_hook(name)
|
70
74
|
_hooks[name]
|
71
75
|
end
|
data/lib/hooks/hook.rb
CHANGED
@@ -50,6 +50,12 @@ module Hooks
|
|
50
50
|
|
51
51
|
private
|
52
52
|
def execute_callback(scope, callback, *args)
|
53
|
+
scope = @options[:scope].(callback, scope) if @options[:scope]
|
54
|
+
|
55
|
+
evaluate_callback(scope, callback, *args)
|
56
|
+
end
|
57
|
+
|
58
|
+
def evaluate_callback(scope, callback, *args)
|
53
59
|
callback.evaluate(scope, *args) # from Uber::Options::Value.
|
54
60
|
end
|
55
61
|
|
data/lib/hooks/version.rb
CHANGED
data/test/hook_test.rb
CHANGED
@@ -9,6 +9,12 @@ class HookTest < MiniTest::Spec
|
|
9
9
|
|
10
10
|
subject.to_a.map(&:to_sym).must_equal [:play_music, :drink_beer]
|
11
11
|
end
|
12
|
+
|
13
|
+
it "evals the procs in the context of its argument" do
|
14
|
+
subject << proc { self }
|
15
|
+
obj = Object.new
|
16
|
+
subject.run(obj).must_equal [obj]
|
17
|
+
end
|
12
18
|
end
|
13
19
|
|
14
20
|
class ResultsTest < MiniTest::Spec
|
@@ -28,4 +34,4 @@ class ResultsTest < MiniTest::Spec
|
|
28
34
|
subject.not_halted?.must_equal true
|
29
35
|
end
|
30
36
|
end
|
31
|
-
end
|
37
|
+
end
|
data/test/hooks_test.rb
CHANGED
@@ -89,10 +89,26 @@ class HooksTest < MiniTest::Spec
|
|
89
89
|
assert_equal [2, 0], subject.executed
|
90
90
|
end
|
91
91
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
describe "the define-time scope option" do
|
93
|
+
it "execute block callbacks in instance context when not defined with a scope option" do
|
94
|
+
subject.class.after_eight { executed << :c }
|
95
|
+
subject.run_hook(:after_eight)
|
96
|
+
assert_equal [:c], subject.executed
|
97
|
+
end
|
98
|
+
|
99
|
+
it ":scope receives callback and original scope" do
|
100
|
+
subject.class.define_hook(:scoped_hook, scope: lambda { |callback, scope| [[callback], [scope]] })
|
101
|
+
hook = subject.class._hooks[:scoped_hook]
|
102
|
+
subject.class.scoped_hook :flatten # call [[callback], [scope]].flatten
|
103
|
+
subject.run_hook(:scoped_hook).must_equal [[hook.last, subject]]
|
104
|
+
end
|
105
|
+
|
106
|
+
it "evaluates procs in their original context when :scope returns nil" do
|
107
|
+
subject.class.define_hook(
|
108
|
+
:scoped_hook, scope: lambda { |callback, scope| nil })
|
109
|
+
subject.class.scoped_hook(lambda { self })
|
110
|
+
subject.run_hook(:scoped_hook).must_equal [self]
|
111
|
+
end
|
96
112
|
end
|
97
113
|
|
98
114
|
it "returns all callbacks in order" do
|
@@ -213,4 +229,4 @@ class HookSetTest < MiniTest::Spec
|
|
213
229
|
clone.must_equal(:after_eight => [first_hook, second_hook])
|
214
230
|
end
|
215
231
|
# TODO: test if options get cloned.
|
216
|
-
end
|
232
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hooks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uber
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.0.
|
19
|
+
version: 0.0.14
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.0.
|
26
|
+
version: 0.0.14
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 5.
|
33
|
+
version: 5.4.1
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 5.
|
40
|
+
version: 5.4.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -97,12 +97,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
99
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.
|
100
|
+
rubygems_version: 2.4.8
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: Generic hooks with callbacks for Ruby.
|
104
|
-
test_files:
|
105
|
-
|
106
|
-
- test/hooks_test.rb
|
107
|
-
- test/instance_hooks_test.rb
|
108
|
-
- test/test_helper.rb
|
104
|
+
test_files: []
|
105
|
+
has_rdoc:
|