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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a443efe3e8187d217fc45ebb0dfa46ec5ebfab70
4
- data.tar.gz: 6eef3c4bc347fdc56b8f4ffc4a9586bf25f4ae5a
3
+ metadata.gz: 9fad249f87b5002ed27f3174c9268cad5f00be86
4
+ data.tar.gz: 5beade083dcdc44fb292bfef06018e9fec522a50
5
5
  SHA512:
6
- metadata.gz: 4f99392b4f3de07d945061ec605de9152f635589a140898c9e45df368395c8b7ffb025964c4c59dd211d2dace142a00abf792f308ff63ee2e7f6b14d03ec968b
7
- data.tar.gz: d2e0920e00253fbe2ba687f530c3ea223a9f467465fab6ad9ef852a27f6e863e2bc3fa4c0bf25d58cc417a124faa3199d81cf70a149c99cbadc731a6dc9ba976
6
+ metadata.gz: c883e89e33556a57c0df0e375d0be763edad476b7a9b0ef013397c1a7990d36d7d82a50549944d9818778969745e6d069da8af7ab1661f2c6b5e2e343f3cf4ef
7
+ data.tar.gz: f61d4bddc22992f25d705332bb8ad729f2c32d5ba3fd46af901501510b53b7a85a40fe02c588c66d36753cdabb7506bcea29ce393f5d3e6562c0fef3b797955c
@@ -1,5 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
3
  - 1.9.3
5
4
  - 2.0.0
5
+ - 2.2.0
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.4.1
2
+
3
+ * Introduce `define_hook .., scope: ->{}` option. This allows changing the callback execution context per callback. Many thanks to @doudou for pushing this.
4
+
1
5
  ## 0.4.0
2
6
 
3
7
  * Same as 0.3.6 but we wanna indicate an internal change (using uber).
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.
@@ -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 = "MIT"
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.4"
21
+ s.add_dependency "uber", "~> 0.0.14"
23
22
 
24
- s.add_development_dependency "minitest", ">= 5.0.0"
23
+ s.add_development_dependency "minitest", ">= 5.4.1"
25
24
  s.add_development_dependency "rake"
26
25
  end
@@ -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
- # instance_exec(self, &callback)
69
+ # callback.evaluate(self, "create")
66
70
  # end
67
71
  #
68
- # would run callbacks in the object _instance_ context, passing +self+ as block parameter.
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
@@ -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
 
@@ -1,3 +1,3 @@
1
1
  module Hooks
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -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
@@ -89,10 +89,26 @@ class HooksTest < MiniTest::Spec
89
89
  assert_equal [2, 0], subject.executed
90
90
  end
91
91
 
92
- it "execute block callbacks in instance context" do
93
- subject.class.after_eight { executed << :c }
94
- subject.run_hook(:after_eight)
95
- assert_equal [:c], subject.executed
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.0
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: 2014-03-11 00:00:00.000000000 Z
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.4
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.4
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.0.0
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.0.0
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.2.1
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
- - test/hook_test.rb
106
- - test/hooks_test.rb
107
- - test/instance_hooks_test.rb
108
- - test/test_helper.rb
104
+ test_files: []
105
+ has_rdoc: