has-guarded-handlers 1.6.0 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/Gemfile +0 -5
- data/README.md +30 -4
- data/Rakefile +0 -2
- data/has-guarded-handlers.gemspec +1 -2
- data/lib/has_guarded_handlers.rb +42 -1
- data/lib/has_guarded_handlers/version.rb +1 -1
- data/spec/has_guarded_handlers_spec.rb +122 -82
- metadata +7 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f951b34654f8fa653a3b1cda2ef2c54b6e62ad9
|
4
|
+
data.tar.gz: 8b78f49afa001bcc62a169ae9a372ac2574ac82c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1f097330d572d3eadd22f03dae81c22d6bd8dfc49c29104b5aadcfb46f936cb3a1ef8470b5093752f66a892dc17e8b395b642947ddee38480099f31818f6060
|
7
|
+
data.tar.gz: 57c2a94053e64b871a32d15367ba531e8f5294a6760439d4e73200be1ba44a059f73dd91d2499a5dae2542aadbe3114f151bcf12846c2166eb7844245065ebd8
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# [develop](https://github.com/adhearsion/has-guarded-handlers)
|
2
2
|
|
3
|
+
# [1.6.2](https://github.com/adhearsion/has-guarded-handlers/compare/v1.6.1...v1.6.2) - [2015-02-20](https://rubygems.org/gems/has-guarded-handlers/versions/1.6.2)
|
4
|
+
* Bugfix: Fix release pipeline
|
5
|
+
|
6
|
+
# [1.6.1](https://github.com/adhearsion/has-guarded-handlers/compare/v1.6.0...v1.6.1) - [2015-02-20](https://rubygems.org/gems/has-guarded-handlers/versions/1.6.1)
|
7
|
+
* Bugfix: Ensure temp handlers are executed only once.
|
8
|
+
* Improved documentation on how chained handlers execute via `throw :pass`, `throw :halt`, and not explicitly throwing.
|
9
|
+
|
3
10
|
# [1.6.0](https://github.com/adhearsion/has-guarded-handlers/compare/v1.5.0...v1.6.0) - [2014-01-16](https://rubygems.org/gems/has-guarded-handlers/versions/1.6.0)
|
4
11
|
* Feature: Add a broadcast mode to handler triggering, which ignores what happens in handlers (return value and exceptions) and unconditionally continues the handler chain.
|
5
12
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,12 @@
|
|
1
1
|
# HasGuardedHandlers
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/has-guarded-handlers.png)](https://rubygems.org/gems/has-guarded-handlers)
|
4
|
+
[![Build Status](https://secure.travis-ci.org/adhearsion/has-guarded-handlers.png?branch=develop)](http://travis-ci.org/adhearsion/has-guarded-handlers)
|
5
|
+
[![Dependency Status](https://gemnasium.com/adhearsion/has-guarded-handlers.png?travis)](https://gemnasium.com/adhearsion/has-guarded-handlers)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/adhearsion/has-guarded-handlers.png)](https://codeclimate.com/github/adhearsion/has-guarded-handlers)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/adhearsion/has-guarded-handlers/badge.png?branch=develop)](https://coveralls.io/r/adhearsion/has-guarded-handlers)
|
8
|
+
[![Inline docs](http://inch-ci.org/github/adhearsion/has-guarded-handlers.png?branch=develop)](http://inch-ci.org/github/adhearsion/has-guarded-handlers)
|
9
|
+
|
2
10
|
HasGuardedHandlers allows an object's API to provide flexible handler registration, storage and matching to arbitrary events.
|
3
11
|
|
4
12
|
## Installation
|
@@ -78,12 +86,12 @@ a.register_handler_with_options(:event, {:tmp => true, :priority => 10}, :foo =>
|
|
78
86
|
|
79
87
|
### Handler chaining
|
80
88
|
|
81
|
-
|
89
|
+
Each handler can control whether subsequent handlers should be executed by throwing `:pass` or `:halt`.
|
82
90
|
|
83
|
-
|
91
|
+
To explicitly pass to the next handler, throw `:pass` in your handler:
|
84
92
|
|
85
93
|
```ruby
|
86
|
-
a.register_handler(:event) { throw :pass }
|
94
|
+
a.register_handler(:event) { do_stuff; throw :pass }
|
87
95
|
a.register_handler(:event) { ... } # This will be executed
|
88
96
|
|
89
97
|
a.trigger_handler :event, :foo
|
@@ -92,12 +100,30 @@ a.trigger_handler :event, :foo
|
|
92
100
|
or indeed explicitly halt the handler chain by throwing `:halt` in the handler:
|
93
101
|
|
94
102
|
```ruby
|
95
|
-
a.register_handler(:event) { throw :halt }
|
103
|
+
a.register_handler(:event) { do_stuff; throw :halt }
|
96
104
|
a.register_handler(:event) { ... } # This will not be executed
|
97
105
|
|
98
106
|
a.trigger_handler :event, :foo
|
99
107
|
```
|
100
108
|
|
109
|
+
If nothing is thrown in the event handler, the handler chain will be halted by default, so subsequent handlers will not be executed.
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
a.register_handler(:event) { do_stuff; }
|
113
|
+
a.register_handler(:event) { ... } # This will not be executed
|
114
|
+
|
115
|
+
a.trigger_handler :event, :foo
|
116
|
+
```
|
117
|
+
|
118
|
+
By triggering the event in broadcast mode, the handler chain will continue by default.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
a.register_handler(:event) { do_stuff; }
|
122
|
+
a.register_handler(:event) { ... } # This will be executed
|
123
|
+
|
124
|
+
a.trigger_handler :event, :foo, broadcast: true
|
125
|
+
```
|
126
|
+
|
101
127
|
### What are guards?
|
102
128
|
|
103
129
|
Guards are a concept borrowed from Erlang. They help to better compartmentalise handlers.
|
data/Rakefile
CHANGED
@@ -3,7 +3,6 @@ Bundler::GemHelper.install_tasks
|
|
3
3
|
|
4
4
|
require 'rspec/core'
|
5
5
|
require 'rspec/core/rake_task'
|
6
|
-
require 'ci/reporter/rake/rspec'
|
7
6
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
7
|
spec.pattern = 'spec/**/*_spec.rb'
|
9
8
|
spec.rspec_opts = '--color'
|
@@ -16,7 +15,6 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
|
|
16
15
|
end
|
17
16
|
|
18
17
|
task :default => :spec
|
19
|
-
task :ci => ['ci:setup:rspec', :spec]
|
20
18
|
|
21
19
|
require 'yard'
|
22
20
|
YARD::Rake::YardocTask.new
|
@@ -19,8 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
21
|
s.add_development_dependency 'bundler', ["~> 1.0"]
|
22
|
-
s.add_development_dependency 'rspec', ["
|
23
|
-
s.add_development_dependency 'ci_reporter', [">= 1.6.3"]
|
22
|
+
s.add_development_dependency 'rspec', ["~> 3.0"]
|
24
23
|
s.add_development_dependency 'yard', [">= 0.7.0"]
|
25
24
|
s.add_development_dependency 'rake', [">= 0"]
|
26
25
|
s.add_development_dependency 'guard-rspec'
|
data/lib/has_guarded_handlers.rb
CHANGED
@@ -1,6 +1,47 @@
|
|
1
1
|
require "has_guarded_handlers/version"
|
2
2
|
require 'securerandom'
|
3
3
|
|
4
|
+
#
|
5
|
+
# HasGuardedHandlers allows an object's API to provide flexible handler registration, storage and matching to arbitrary events.
|
6
|
+
#
|
7
|
+
# HasGuardedHandlers is a module that should be mixed into some object which needs to emit events.
|
8
|
+
#
|
9
|
+
# See the README for more usage info.
|
10
|
+
#
|
11
|
+
# @author Ben Langfeld <ben@langfeld.me>
|
12
|
+
#
|
13
|
+
# @example Simple usage
|
14
|
+
#
|
15
|
+
# require 'has_guarded_handlers'
|
16
|
+
#
|
17
|
+
# class A
|
18
|
+
# include HasGuardedHandlers
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# a = A.new
|
22
|
+
# a.register_handler :event do |event|
|
23
|
+
# puts "Handled the event #{event.inspect}"
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# a.trigger_handler :event, "Foo!"
|
27
|
+
#
|
28
|
+
# @example Guarding event handlers
|
29
|
+
#
|
30
|
+
# require 'has_guarded_handlers'
|
31
|
+
#
|
32
|
+
# class A
|
33
|
+
# include HasGuardedHandlers
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# a = A.new
|
37
|
+
# a.register_handler :event, :type => :foo do |event|
|
38
|
+
# puts "Handled the event of type #{event.type} with value #{event.value}"
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# Event = Class.new Struct.new(:type, :value)
|
42
|
+
#
|
43
|
+
# a.trigger_handler :event, Event.new(:foo, 'bar')
|
44
|
+
#
|
4
45
|
module HasGuardedHandlers
|
5
46
|
# Register a handler
|
6
47
|
#
|
@@ -104,7 +145,7 @@ module HasGuardedHandlers
|
|
104
145
|
true unless broadcast
|
105
146
|
end
|
106
147
|
end
|
107
|
-
delete_handler_if(type) { |_, h, _| h.equal? handler } if tmp &&
|
148
|
+
delete_handler_if(type) { |_, h, _| h.equal? handler } if tmp && called
|
108
149
|
val
|
109
150
|
end
|
110
151
|
end
|
@@ -11,39 +11,79 @@ describe HasGuardedHandlers do
|
|
11
11
|
let(:response) { double 'Response' }
|
12
12
|
|
13
13
|
it 'can register a handler' do
|
14
|
-
response.
|
14
|
+
expect(response).to receive(:call).twice.with(event)
|
15
15
|
subject.register_handler(:event) { |e| response.call e }
|
16
|
-
subject.trigger_handler(:event, event).
|
17
|
-
subject.trigger_handler(:event, event).
|
16
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
17
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'can register a handler for all events, regardless of category' do
|
21
|
-
response.
|
21
|
+
expect(response).to receive(:call).twice.with(event)
|
22
22
|
subject.register_handler { |e| response.call e }
|
23
23
|
subject.trigger_handler :event, event
|
24
24
|
subject.trigger_handler :bah, event
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
context 'when a one-shot (tmp) handler is registered' do
|
28
|
+
it 'can register a one-shot (tmp) handler' do
|
29
|
+
expect(response).to receive(:call).exactly(3).times.with(event)
|
30
|
+
expect(event).to receive(:foo).exactly(3).times.and_return :bar
|
30
31
|
|
31
|
-
|
32
|
-
|
32
|
+
nomatch_event = double 'Event(nomatch)'
|
33
|
+
expect(nomatch_event).to receive(:foo).twice.and_return :baz
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
subject.register_handler(:event, :foo => :bar) do |e|
|
36
|
+
response.call e
|
37
|
+
throw :pass
|
38
|
+
end
|
39
|
+
subject.register_tmp_handler(:event, :foo => :bar) { |e| response.call e }
|
40
|
+
|
41
|
+
expect(subject.trigger_handler(:event, nomatch_event)).to be false
|
42
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
43
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
37
44
|
end
|
38
|
-
subject.register_tmp_handler(:event, :foo => :bar) { |e| response.call e }
|
39
45
|
|
40
|
-
|
41
|
-
|
42
|
-
|
46
|
+
it 'is executed once regardless of return value' do
|
47
|
+
expect(response).to receive(:call).exactly(3).times.with(event)
|
48
|
+
expect(event).to receive(:foo).exactly(3).times.and_return :bar
|
49
|
+
|
50
|
+
subject.register_tmp_handler(:event, :foo => :bar) do |e|
|
51
|
+
response.call e
|
52
|
+
throw :pass
|
53
|
+
end
|
54
|
+
subject.register_tmp_handler(:event, :foo => :bar) do |e|
|
55
|
+
response.call e
|
56
|
+
false
|
57
|
+
end
|
58
|
+
subject.register_tmp_handler(:event, :foo => :bar) do |e|
|
59
|
+
response.call e
|
60
|
+
true
|
61
|
+
end
|
62
|
+
|
63
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
64
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
65
|
+
expect(subject.trigger_handler(:event, event)).to be false
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'does not remove the handler until executed' do
|
69
|
+
expect(response).to receive(:call).twice
|
70
|
+
expect(event).to receive(:foo).twice.and_return :bar
|
71
|
+
|
72
|
+
second_event = double 'Event(nomatch)'
|
73
|
+
expect(second_event).to receive(:foo).once.and_return :baz
|
74
|
+
|
75
|
+
subject.register_tmp_handler(:event, :foo => :bar) { |e| response.call e }
|
76
|
+
subject.register_tmp_handler(:event, :foo => :baz) { |e| response.call e }
|
77
|
+
|
78
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
79
|
+
expect(subject.trigger_handler(:event, event)).to be false
|
80
|
+
expect(subject.trigger_handler(:event, second_event)).to be true
|
81
|
+
expect(subject.trigger_handler(:event, second_event)).to be false
|
82
|
+
end
|
43
83
|
end
|
44
84
|
|
45
85
|
it 'can unregister a handler after registration' do
|
46
|
-
response.
|
86
|
+
expect(response).to receive(:call).once.with(event)
|
47
87
|
subject.register_handler(:event) { |e| response.call e }
|
48
88
|
id = subject.register_handler(:event) { |e| response.call :foo }
|
49
89
|
subject.unregister_handler :event, id
|
@@ -51,26 +91,26 @@ describe HasGuardedHandlers do
|
|
51
91
|
end
|
52
92
|
|
53
93
|
it 'does not fail when no handlers are set' do
|
54
|
-
lambda do
|
94
|
+
expect(lambda do
|
55
95
|
subject.trigger_handler :event, event
|
56
|
-
end.
|
57
|
-
subject.trigger_handler(:event, event).
|
96
|
+
end).to_not raise_error
|
97
|
+
expect(subject.trigger_handler(:event, event)).to be false
|
58
98
|
end
|
59
99
|
|
60
100
|
it 'allows for breaking out of handlers' do
|
61
|
-
response.
|
62
|
-
response.
|
101
|
+
expect(response).to receive(:handle).once
|
102
|
+
expect(response).to receive(:fail).never
|
63
103
|
subject.register_handler :event do |_|
|
64
104
|
response.handle
|
65
105
|
throw :halt
|
66
106
|
response.fail
|
67
107
|
end
|
68
|
-
subject.trigger_handler(:event, event).
|
108
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
69
109
|
end
|
70
110
|
|
71
111
|
context 'when multiple handlers are registered' do
|
72
112
|
it "stops at the first matching handler regardless of return value" do
|
73
|
-
response.
|
113
|
+
expect(response).to receive(:handle).once
|
74
114
|
subject.register_handler :event do |_|
|
75
115
|
response.handle
|
76
116
|
false
|
@@ -78,12 +118,12 @@ describe HasGuardedHandlers do
|
|
78
118
|
subject.register_handler :event do |_|
|
79
119
|
response.handle
|
80
120
|
end
|
81
|
-
subject.trigger_handler(:event, event).
|
121
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
82
122
|
end
|
83
123
|
|
84
124
|
context "and an early one raises" do
|
85
125
|
it "raises that exception, and does not execute later handlers" do
|
86
|
-
response.
|
126
|
+
expect(response).to receive(:handle).never
|
87
127
|
subject.register_handler :event do |_|
|
88
128
|
raise "Oops"
|
89
129
|
end
|
@@ -96,19 +136,19 @@ describe HasGuardedHandlers do
|
|
96
136
|
|
97
137
|
context "when broadcast mode is enabled on trigger" do
|
98
138
|
it "continues regardless of return value" do
|
99
|
-
response.
|
139
|
+
expect(response).to receive(:handle).twice
|
100
140
|
subject.register_handler :event do |_|
|
101
141
|
response.handle
|
102
142
|
end
|
103
143
|
subject.register_handler :event do |_|
|
104
144
|
response.handle
|
105
145
|
end
|
106
|
-
subject.trigger_handler(:event, event, broadcast: true).
|
146
|
+
expect(subject.trigger_handler(:event, event, broadcast: true)).to be true
|
107
147
|
end
|
108
148
|
|
109
149
|
context "and an early one raises" do
|
110
150
|
it "swallows that exception, and executes later handlers" do
|
111
|
-
response.
|
151
|
+
expect(response).to receive(:handle).once
|
112
152
|
subject.register_handler :event do |_|
|
113
153
|
raise "Oops"
|
114
154
|
end
|
@@ -120,11 +160,11 @@ describe HasGuardedHandlers do
|
|
120
160
|
|
121
161
|
it "can invoke a callback on an exception" do
|
122
162
|
exception_callback = double 'Exception Callback'
|
123
|
-
exception_callback.
|
124
|
-
e.
|
125
|
-
e.message.
|
163
|
+
expect(exception_callback).to receive(:call).once.with(RuntimeError) do |e|
|
164
|
+
expect(e).to be_a(RuntimeError)
|
165
|
+
expect(e.message).to eq "Oops"
|
126
166
|
end.ordered
|
127
|
-
response.
|
167
|
+
expect(response).to receive(:handle).once.ordered
|
128
168
|
subject.register_handler :event do |_|
|
129
169
|
raise "Oops"
|
130
170
|
end
|
@@ -138,9 +178,9 @@ describe HasGuardedHandlers do
|
|
138
178
|
end
|
139
179
|
|
140
180
|
it 'allows for passing to the next handler of the same type' do
|
141
|
-
response.
|
142
|
-
response.
|
143
|
-
response.
|
181
|
+
expect(response).to receive(:handle1).once
|
182
|
+
expect(response).to receive(:handle2).once
|
183
|
+
expect(response).to receive(:fail).never
|
144
184
|
subject.register_handler :event do |_|
|
145
185
|
response.handle1
|
146
186
|
throw :pass
|
@@ -149,27 +189,27 @@ describe HasGuardedHandlers do
|
|
149
189
|
subject.register_handler :event do |_|
|
150
190
|
response.handle2
|
151
191
|
end
|
152
|
-
subject.trigger_handler(:event, event).
|
192
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
153
193
|
end
|
154
194
|
|
155
195
|
context 'when there is nothing to pass to' do
|
156
196
|
it 'correctly indicates that a handler was called' do
|
157
|
-
response.
|
158
|
-
response.
|
197
|
+
expect(response).to receive(:handle1).once
|
198
|
+
expect(response).to receive(:fail).never
|
159
199
|
subject.register_handler :event do |_|
|
160
200
|
response.handle1
|
161
201
|
throw :pass
|
162
202
|
response.fail
|
163
203
|
end
|
164
|
-
subject.trigger_handler(:event, event).
|
204
|
+
expect(subject.trigger_handler(:event, event)).to be true
|
165
205
|
end
|
166
206
|
end
|
167
207
|
|
168
208
|
describe 'when registering handlers with the same priority' do
|
169
209
|
it 'preserves the order of specification of the handlers' do
|
170
|
-
response.
|
171
|
-
response.
|
172
|
-
response.
|
210
|
+
expect(response).to receive(:handle1).once.ordered
|
211
|
+
expect(response).to receive(:handle2).once.ordered
|
212
|
+
expect(response).to receive(:handle3).once.ordered
|
173
213
|
subject.register_handler :event do |_|
|
174
214
|
response.handle1
|
175
215
|
throw :pass
|
@@ -188,9 +228,9 @@ describe HasGuardedHandlers do
|
|
188
228
|
|
189
229
|
describe 'when registering handlers with a specified priority' do
|
190
230
|
it 'executes handlers in that order' do
|
191
|
-
response.
|
192
|
-
response.
|
193
|
-
response.
|
231
|
+
expect(response).to receive(:handle1).once.ordered
|
232
|
+
expect(response).to receive(:handle2).once.ordered
|
233
|
+
expect(response).to receive(:handle3).once.ordered
|
194
234
|
subject.register_handler_with_priority :event, -10 do |_|
|
195
235
|
response.handle3
|
196
236
|
throw :pass
|
@@ -208,7 +248,7 @@ describe HasGuardedHandlers do
|
|
208
248
|
end
|
209
249
|
|
210
250
|
it 'can clear handlers' do
|
211
|
-
response.
|
251
|
+
expect(response).to receive(:call).once
|
212
252
|
|
213
253
|
subject.register_handler(:event) { |_| response.call }
|
214
254
|
subject.trigger_handler :event, event
|
@@ -224,7 +264,7 @@ describe HasGuardedHandlers do
|
|
224
264
|
end
|
225
265
|
|
226
266
|
it 'can be a class' do
|
227
|
-
response.
|
267
|
+
expect(response).to receive(:call).once
|
228
268
|
subject.register_handler(:event, GuardedObject) { |_| response.call }
|
229
269
|
|
230
270
|
subject.trigger_handler :event, GuardedObject.new
|
@@ -232,7 +272,7 @@ describe HasGuardedHandlers do
|
|
232
272
|
end
|
233
273
|
|
234
274
|
it 'can be a module' do
|
235
|
-
response.
|
275
|
+
expect(response).to receive(:call).once
|
236
276
|
subject.register_handler(:event, GuardMixin) { |_| response.call }
|
237
277
|
|
238
278
|
subject.trigger_handler :event, GuardedObject.new
|
@@ -240,54 +280,54 @@ describe HasGuardedHandlers do
|
|
240
280
|
end
|
241
281
|
|
242
282
|
it 'can be a symbol' do
|
243
|
-
response.
|
283
|
+
expect(response).to receive(:call).once
|
244
284
|
subject.register_handler(:event, :chat?) { |_| response.call }
|
245
285
|
|
246
|
-
event.
|
286
|
+
expect(event).to receive(:chat?).and_return true
|
247
287
|
subject.trigger_handler :event, event
|
248
288
|
|
249
|
-
event.
|
289
|
+
expect(event).to receive(:chat?).and_return false
|
250
290
|
subject.trigger_handler :event, event
|
251
291
|
end
|
252
292
|
|
253
293
|
it 'can be a hash with string match' do
|
254
|
-
response.
|
294
|
+
expect(response).to receive(:call).once
|
255
295
|
subject.register_handler(:event, :body => 'exit') { |_| response.call }
|
256
296
|
|
257
|
-
event.
|
297
|
+
expect(event).to receive(:body).and_return 'exit'
|
258
298
|
subject.trigger_handler :event, event
|
259
299
|
|
260
|
-
event.
|
300
|
+
expect(event).to receive(:body).and_return 'not-exit'
|
261
301
|
subject.trigger_handler :event, event
|
262
302
|
end
|
263
303
|
|
264
304
|
it 'can be a hash with a value' do
|
265
|
-
response.
|
305
|
+
expect(response).to receive(:call).once
|
266
306
|
subject.register_handler(:event, :number => 0) { |_| response.call }
|
267
307
|
|
268
|
-
event.
|
308
|
+
expect(event).to receive(:number).and_return 0
|
269
309
|
subject.trigger_handler :event, event
|
270
310
|
|
271
|
-
event.
|
311
|
+
expect(event).to receive(:number).and_return 1
|
272
312
|
subject.trigger_handler :event, event
|
273
313
|
end
|
274
314
|
|
275
315
|
it 'can be a hash with a regexp' do
|
276
|
-
response.
|
316
|
+
expect(response).to receive(:call).once
|
277
317
|
subject.register_handler(:event, :body => /exit/) { |_| response.call }
|
278
318
|
|
279
|
-
event.
|
319
|
+
expect(event).to receive(:body).and_return 'more than just exit, but exit still'
|
280
320
|
subject.trigger_handler :event, event
|
281
321
|
|
282
|
-
event.
|
322
|
+
expect(event).to receive(:body).and_return 'keyword not found'
|
283
323
|
subject.trigger_handler :event, event
|
284
324
|
|
285
|
-
event.
|
325
|
+
expect(event).to receive(:body).and_return nil
|
286
326
|
subject.trigger_handler :event, event
|
287
327
|
end
|
288
328
|
|
289
329
|
it 'can be a hash with arguments' do
|
290
|
-
response.
|
330
|
+
expect(response).to receive(:call).once
|
291
331
|
subject.register_handler(:event, [:[], :foo] => :bar) { |_| response.call }
|
292
332
|
|
293
333
|
subject.trigger_handler :event, {:foo => :bar}
|
@@ -296,67 +336,67 @@ describe HasGuardedHandlers do
|
|
296
336
|
end
|
297
337
|
|
298
338
|
it 'can be a hash with an array' do
|
299
|
-
response.
|
339
|
+
expect(response).to receive(:call).twice
|
300
340
|
subject.register_handler(:event, :type => [:result, :error]) { |_| response.call }
|
301
341
|
|
302
342
|
event = double 'Event'
|
303
|
-
event.
|
343
|
+
expect(event).to receive(:type).at_least(1).and_return :result
|
304
344
|
subject.trigger_handler :event, event
|
305
345
|
|
306
346
|
event = double 'Event'
|
307
|
-
event.
|
347
|
+
expect(event).to receive(:type).at_least(1).and_return :error
|
308
348
|
subject.trigger_handler :event, event
|
309
349
|
|
310
350
|
event = double 'Event'
|
311
|
-
event.
|
351
|
+
expect(event).to receive(:type).at_least(1).and_return :get
|
312
352
|
subject.trigger_handler :event, event
|
313
353
|
end
|
314
354
|
|
315
355
|
it 'chained are treated like andand (short circuited)' do
|
316
|
-
response.
|
356
|
+
expect(response).to receive(:call).once
|
317
357
|
subject.register_handler(:event, :type => :get, :body => 'test') { |_| response.call }
|
318
358
|
|
319
359
|
event = double 'Event'
|
320
|
-
event.
|
321
|
-
event.
|
360
|
+
expect(event).to receive(:type).at_least(1).and_return :get
|
361
|
+
expect(event).to receive(:body).and_return 'test'
|
322
362
|
subject.trigger_handler :event, event
|
323
363
|
|
324
364
|
event = double 'Event'
|
325
|
-
event.
|
326
|
-
event.
|
365
|
+
expect(event).to receive(:type).at_least(1).and_return :set
|
366
|
+
expect(event).to receive(:body).never
|
327
367
|
subject.trigger_handler :event, event
|
328
368
|
end
|
329
369
|
|
330
370
|
it 'within an Array are treated as oror (short circuited)' do
|
331
|
-
response.
|
371
|
+
expect(response).to receive(:call).twice
|
332
372
|
subject.register_handler(:event, [{:type => :get}, {:body => 'test'}]) { |_| response.call }
|
333
373
|
|
334
374
|
event = double 'Event'
|
335
|
-
event.
|
336
|
-
event.
|
375
|
+
expect(event).to receive(:type).at_least(1).and_return :set
|
376
|
+
expect(event).to receive(:body).and_return 'test'
|
337
377
|
subject.trigger_handler :event, event
|
338
378
|
|
339
379
|
event = double 'Event'
|
340
|
-
event.
|
341
|
-
event.
|
380
|
+
expect(event).to receive(:type).at_least(1).and_return :get
|
381
|
+
expect(event).to receive(:body).never
|
342
382
|
subject.trigger_handler :event, event
|
343
383
|
end
|
344
384
|
|
345
385
|
it 'can be a lambda' do
|
346
|
-
response.
|
386
|
+
expect(response).to receive(:call).once
|
347
387
|
subject.register_handler(:event, lambda { |e| e.number % 3 == 0 }) { |_| response.call }
|
348
388
|
|
349
|
-
event.
|
389
|
+
expect(event).to receive(:number).once.and_return 3
|
350
390
|
subject.trigger_handler :event, event
|
351
391
|
|
352
|
-
event.
|
392
|
+
expect(event).to receive(:number).once.and_return 2
|
353
393
|
subject.trigger_handler :event, event
|
354
394
|
end
|
355
395
|
|
356
396
|
it 'raises an error when a bad guard is tried' do
|
357
|
-
lambda {
|
397
|
+
expect(lambda {
|
358
398
|
subject.register_handler(:event, 0) {}
|
359
|
-
}.
|
399
|
+
}).to raise_error RuntimeError
|
360
400
|
end
|
361
401
|
end
|
362
402
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has-guarded-handlers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Langfeld
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-02-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -29,30 +29,16 @@ dependencies:
|
|
29
29
|
name: rspec
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- - "
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: 2.5.0
|
35
|
-
type: :development
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - ">="
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
version: 2.5.0
|
42
|
-
- !ruby/object:Gem::Dependency
|
43
|
-
name: ci_reporter
|
44
|
-
requirement: !ruby/object:Gem::Requirement
|
45
|
-
requirements:
|
46
|
-
- - ">="
|
32
|
+
- - "~>"
|
47
33
|
- !ruby/object:Gem::Version
|
48
|
-
version:
|
34
|
+
version: '3.0'
|
49
35
|
type: :development
|
50
36
|
prerelease: false
|
51
37
|
version_requirements: !ruby/object:Gem::Requirement
|
52
38
|
requirements:
|
53
|
-
- - "
|
39
|
+
- - "~>"
|
54
40
|
- !ruby/object:Gem::Version
|
55
|
-
version:
|
41
|
+
version: '3.0'
|
56
42
|
- !ruby/object:Gem::Dependency
|
57
43
|
name: yard
|
58
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
122
|
version: '0'
|
137
123
|
requirements: []
|
138
124
|
rubyforge_project: has-guarded-handlers
|
139
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.4.5
|
140
126
|
signing_key:
|
141
127
|
specification_version: 4
|
142
128
|
summary: A library for associating a set of event handlers, complete with guards,
|