dry-events 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +9 -7
- data/CHANGELOG.md +39 -1
- data/Gemfile +2 -1
- data/README.md +8 -10
- data/lib/dry/events/bus.rb +21 -9
- data/lib/dry/events/event.rb +18 -9
- data/lib/dry/events/filter.rb +72 -0
- data/lib/dry/events/listener.rb +1 -1
- data/lib/dry/events/publisher.rb +32 -11
- data/lib/dry/events/version.rb +1 -1
- data/spec/spec_helper.rb +4 -1
- data/spec/unit/dry/events/event_spec.rb +66 -0
- data/spec/unit/dry/events/filter_spec.rb +74 -0
- data/spec/unit/dry/events/listener_spec.rb +18 -4
- data/spec/unit/dry/events/publisher_spec.rb +22 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1423fe7cb9ad77d3af4fea999666f1ff275204e5dad84e4ba8f83ea230b0e2d0
|
4
|
+
data.tar.gz: 30a0f8b923a890af19b9937fbf502b51c8a568b33444bba0f3d09995045a016d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d985af03635dea492d89ae37dd5935aa4576d92ca2201bbf4096d12a396df7857fd0007bf7a28275f7d5a985560ef26532ca977bf1ea05e04e815d88db02bd8f
|
7
|
+
data.tar.gz: 8de1e80e33a2a5429fddce3a675ed9c72ae5224bc919d6fcab5300f4f8d7b11617d18330b0af5451884e76accbc755b37d4282155c0aa52dbddd380e1cc723af
|
data/.travis.yml
CHANGED
@@ -2,21 +2,23 @@ language: ruby
|
|
2
2
|
cache: bundler
|
3
3
|
bundler_args: --without benchmarks console tools
|
4
4
|
script: "bundle exec rake ci"
|
5
|
-
before_install: gem update --system
|
6
5
|
after_success:
|
7
6
|
- '[ "${TRAVIS_JOB_NUMBER#*.}" = "1" ] && [ "$TRAVIS_BRANCH" = "master" ] && bundle exec codeclimate-test-reporter'
|
8
7
|
rvm:
|
9
|
-
- 2.
|
10
|
-
- 2.
|
11
|
-
- 2.
|
12
|
-
- jruby-9.
|
8
|
+
- 2.6.3
|
9
|
+
- 2.5.5
|
10
|
+
- 2.4.6
|
11
|
+
- jruby-9.2.7.0
|
12
|
+
- truffleruby
|
13
|
+
matrix:
|
14
|
+
allow_failures:
|
15
|
+
- rvm: truffleruby
|
16
|
+
- rvm: jruby-9.2.7.0
|
13
17
|
env:
|
14
18
|
global:
|
15
19
|
- COVERAGE='true'
|
16
20
|
notifications:
|
17
21
|
webhooks:
|
18
|
-
urls:
|
19
|
-
- https://webhooks.gitter.im/e/19098b4253a72c9796db
|
20
22
|
on_success: change
|
21
23
|
on_failure: always
|
22
24
|
on_start: false
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,41 @@
|
|
1
|
-
# v0.
|
1
|
+
# v0.2.0 2019-07-24
|
2
|
+
|
3
|
+
## Fixed
|
4
|
+
|
5
|
+
* Trying to subscribe to a non-existant event raises an exception (issue #3) (@GustavoCaso + @mensfeld)
|
6
|
+
|
7
|
+
[Compare v0.1.1...v0.2.0](https://github.com/dry-rb/dry-events/compare/v0.1.1...v0.2.0)
|
8
|
+
|
9
|
+
# v0.1.1 2019-03-22
|
10
|
+
|
11
|
+
## Added
|
12
|
+
|
13
|
+
- Subscription filters can be more complex: nested hash inclusion, array inclusion, and proc checks were added (flash-gordon)
|
14
|
+
```ruby
|
15
|
+
# nested hash check
|
16
|
+
subscribe(:event, logger: { level: :info })
|
17
|
+
# pass
|
18
|
+
trigger(:event, logger: { level: :info, output: :stdin })
|
19
|
+
# filtered out
|
20
|
+
trigger(:event, logger: { level: :debug })
|
21
|
+
trigger(:event, something: :else)
|
22
|
+
|
23
|
+
# array inclusion
|
24
|
+
subscribe(:event, logger: { level: %i(info warn error) })
|
25
|
+
# pass
|
26
|
+
trigger(:event, logger: { level: :info })
|
27
|
+
trigger(:event, logger: { level: :error })
|
28
|
+
trigger(:event, logger: { level: :info, output: :stdin })
|
29
|
+
# filtered out
|
30
|
+
trigger(:event, logger: { level: :debug })
|
31
|
+
|
32
|
+
# proc checks
|
33
|
+
# here acts as array inclusion example
|
34
|
+
subscribe(:event, logger: { level: -> level { %i(info warn error).include?(level) })
|
35
|
+
```
|
36
|
+
|
37
|
+
[Compare v0.1.0...v0.1.1](https://github.com/dry-rb/dry-events/compare/v0.1.0...v0.1.1)
|
38
|
+
|
39
|
+
# v0.1.0 2018-01-02
|
2
40
|
|
3
41
|
First public release
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,26 +1,24 @@
|
|
1
1
|
[gem]: https://rubygems.org/gems/dry-events
|
2
|
-
[travis]: https://travis-ci.
|
3
|
-
[gemnasium]: https://gemnasium.com/dry-rb/dry-events
|
2
|
+
[travis]: https://travis-ci.com/dry-rb/dry-events
|
4
3
|
[codeclimate]: https://codeclimate.com/github/dry-rb/dry-events
|
5
|
-
[
|
4
|
+
[chat]: https://dry-rb.zulipchat.com
|
6
5
|
[inchpages]: http://inch-ci.org/github/dry-rb/dry-events
|
7
6
|
|
8
|
-
# dry-events [![Join the chat at https://
|
7
|
+
# dry-events [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
|
9
8
|
|
10
9
|
[![Gem Version](https://badge.fury.io/rb/dry-events.svg)][gem]
|
11
|
-
[![Build Status](https://travis-ci.
|
12
|
-
[![Dependency Status](https://gemnasium.com/dry-rb/dry-events.svg)][gemnasium]
|
10
|
+
[![Build Status](https://travis-ci.com/dry-rb/dry-events.svg?branch=master)][travis]
|
13
11
|
[![Code Climate](https://codeclimate.com/github/dry-rb/dry-events/badges/gpa.svg)][codeclimate]
|
14
12
|
[![Test Coverage](https://codeclimate.com/github/dry-rb/dry-events/badges/coverage.svg)][codeclimate]
|
15
13
|
[![Inline docs](http://inch-ci.org/github/dry-rb/dry-events.svg?branch=master)][inchpages]
|
16
14
|
|
17
15
|
Standalone pub/sub system.
|
18
16
|
|
19
|
-
##
|
17
|
+
## Links
|
20
18
|
|
21
|
-
|
22
|
-
|
19
|
+
* [User docs](http://dry-rb.org/gems/dry-events)
|
20
|
+
* [API docs](http://rubydoc.info/gems/dry-events)
|
23
21
|
|
24
|
-
##
|
22
|
+
## LICENSE
|
25
23
|
|
26
24
|
See `LICENSE` file.
|
data/lib/dry/events/bus.rb
CHANGED
@@ -18,7 +18,6 @@ module Dry
|
|
18
18
|
|
19
19
|
# Initialize a new event bus
|
20
20
|
#
|
21
|
-
# @param [Symbol] id The bus identifier
|
22
21
|
# @param [Hash] events A hash with events
|
23
22
|
# @param [Hash] listeners A hash with listeners
|
24
23
|
#
|
@@ -29,11 +28,11 @@ module Dry
|
|
29
28
|
end
|
30
29
|
|
31
30
|
# @api private
|
32
|
-
def process(event_id, payload
|
33
|
-
listeners[event_id].each do |
|
31
|
+
def process(event_id, payload)
|
32
|
+
listeners[event_id].each do |listener, filter|
|
34
33
|
event = events[event_id].payload(payload)
|
35
34
|
|
36
|
-
if
|
35
|
+
if filter.(payload)
|
37
36
|
yield(event, listener)
|
38
37
|
end
|
39
38
|
end
|
@@ -47,12 +46,12 @@ module Dry
|
|
47
46
|
end
|
48
47
|
|
49
48
|
# @api private
|
50
|
-
def attach(listener,
|
49
|
+
def attach(listener, filter)
|
51
50
|
events.each do |id, event|
|
52
51
|
meth = event.listener_method
|
53
52
|
|
54
53
|
if listener.respond_to?(meth)
|
55
|
-
listeners[id] << [listener.method(meth),
|
54
|
+
listeners[id] << [listener.method(meth), filter]
|
56
55
|
end
|
57
56
|
end
|
58
57
|
end
|
@@ -68,14 +67,27 @@ module Dry
|
|
68
67
|
end
|
69
68
|
|
70
69
|
# @api private
|
71
|
-
def subscribe(event_id,
|
72
|
-
listeners[event_id] << [block,
|
70
|
+
def subscribe(event_id, filter, &block)
|
71
|
+
listeners[event_id] << [block, filter]
|
73
72
|
self
|
74
73
|
end
|
75
74
|
|
76
75
|
# @api private
|
77
76
|
def subscribed?(listener)
|
78
|
-
listeners.values.any? { |value| value.any? { |
|
77
|
+
listeners.values.any? { |value| value.any? { |block, _| block.equal?(listener) } }
|
78
|
+
end
|
79
|
+
|
80
|
+
# @api private
|
81
|
+
def can_handle?(object_or_event_id)
|
82
|
+
case object_or_event_id
|
83
|
+
when String, Symbol
|
84
|
+
events.key?(object_or_event_id)
|
85
|
+
else
|
86
|
+
events
|
87
|
+
.values
|
88
|
+
.map(&:listener_method)
|
89
|
+
.any?(&object_or_event_id.method(:respond_to?))
|
90
|
+
end
|
79
91
|
end
|
80
92
|
end
|
81
93
|
end
|
data/lib/dry/events/event.rb
CHANGED
@@ -9,22 +9,36 @@ module Dry
|
|
9
9
|
class Event
|
10
10
|
include Dry::Equalizer(:id, :payload)
|
11
11
|
|
12
|
+
InvalidEventNameError = Class.new(StandardError) do
|
13
|
+
# @api private
|
14
|
+
def initialize
|
15
|
+
super("please provide a valid event name, it could be either String or Symbol")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
12
19
|
DOT = '.'.freeze
|
13
20
|
UNDERSCORE = '_'.freeze
|
14
21
|
|
15
22
|
# @!attribute [r] id
|
16
|
-
# @return [Symbol] The event identifier
|
23
|
+
# @return [Symbol, String] The event identifier
|
17
24
|
attr_reader :id
|
18
25
|
|
26
|
+
# @api private
|
27
|
+
def self.new(id, payload = EMPTY_HASH)
|
28
|
+
return super(id, payload) if (id.is_a?(String) || id.is_a?(Symbol)) && !id.empty?
|
29
|
+
|
30
|
+
raise InvalidEventNameError.new
|
31
|
+
end
|
32
|
+
|
19
33
|
# Initialize a new event
|
20
34
|
#
|
21
|
-
# @param [Symbol] id The event identifier
|
22
|
-
# @param [Hash] payload
|
35
|
+
# @param [Symbol, String] id The event identifier
|
36
|
+
# @param [Hash] payload
|
23
37
|
#
|
24
38
|
# @return [Event]
|
25
39
|
#
|
26
40
|
# @api private
|
27
|
-
def initialize(id, payload
|
41
|
+
def initialize(id, payload)
|
28
42
|
@id = id
|
29
43
|
@payload = payload
|
30
44
|
end
|
@@ -66,11 +80,6 @@ module Dry
|
|
66
80
|
end
|
67
81
|
end
|
68
82
|
|
69
|
-
# @api private
|
70
|
-
def trigger?(query)
|
71
|
-
query.empty? || query.all? { |key, value| payload[key] == value }
|
72
|
-
end
|
73
|
-
|
74
83
|
# @api private
|
75
84
|
def listener_method
|
76
85
|
@listener_method ||= :"on_#{id.to_s.gsub(DOT, UNDERSCORE)}"
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module Dry
|
4
|
+
module Events
|
5
|
+
# Event filter
|
6
|
+
#
|
7
|
+
# A filter cherry-picks probes payload of events.
|
8
|
+
# Events not matching the predicates don't fire callbacks.
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
class Filter
|
12
|
+
NO_MATCH = Object.new.freeze
|
13
|
+
|
14
|
+
# @!attribute [r] events
|
15
|
+
# @return [Array] A list of lambdas checking payloads
|
16
|
+
attr_reader :checks
|
17
|
+
|
18
|
+
# Create a new filter
|
19
|
+
#
|
20
|
+
# @param [Hash] filter Source filter
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
def initialize(filter)
|
24
|
+
@checks = build_checks(filter)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Test event payload against the checks
|
28
|
+
#
|
29
|
+
# @param [Hash] payload Event payload
|
30
|
+
#
|
31
|
+
# @api private
|
32
|
+
def call(payload = EMPTY_HASH)
|
33
|
+
checks.all? { |check| check.(payload) }
|
34
|
+
end
|
35
|
+
|
36
|
+
# Recursively build checks
|
37
|
+
#
|
38
|
+
# @api private
|
39
|
+
def build_checks(filter, checks = EMPTY_ARRAY, keys = EMPTY_ARRAY)
|
40
|
+
if filter.is_a?(Hash)
|
41
|
+
filter.reduce(checks) do |cs, (key, value)|
|
42
|
+
build_checks(value, cs, [*keys, key])
|
43
|
+
end
|
44
|
+
else
|
45
|
+
[*checks, method(:compare).curry.(keys, predicate(filter))]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# @api private
|
50
|
+
def compare(path, predicate, payload)
|
51
|
+
value = path.reduce(payload) do |acc, key|
|
52
|
+
if acc.is_a?(Hash) && acc.key?(key)
|
53
|
+
acc[key]
|
54
|
+
else
|
55
|
+
break NO_MATCH
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
predicate.(value)
|
60
|
+
end
|
61
|
+
|
62
|
+
# @api private
|
63
|
+
def predicate(value)
|
64
|
+
case value
|
65
|
+
when Proc then value
|
66
|
+
when Array then value.method(:include?)
|
67
|
+
else value.method(:==)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/dry/events/listener.rb
CHANGED
data/lib/dry/events/publisher.rb
CHANGED
@@ -5,6 +5,7 @@ require 'dry/core/class_attributes'
|
|
5
5
|
require 'dry/events/constants'
|
6
6
|
require 'dry/events/event'
|
7
7
|
require 'dry/events/bus'
|
8
|
+
require 'dry/events/filter'
|
8
9
|
|
9
10
|
module Dry
|
10
11
|
module Events
|
@@ -18,6 +19,19 @@ module Dry
|
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
22
|
+
# @api public
|
23
|
+
InvalidSubscriberError = Class.new(StandardError) do
|
24
|
+
# @api private
|
25
|
+
def initialize(object_or_event_id)
|
26
|
+
case object_or_event_id
|
27
|
+
when String, Symbol
|
28
|
+
super("you are trying to subscribe to an event: `#{object_or_event_id}` that has not been registered")
|
29
|
+
else
|
30
|
+
super("you try use subscriber object that will never be executed")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
21
35
|
# Extension used for classes that can publish events
|
22
36
|
#
|
23
37
|
# @example
|
@@ -116,13 +130,13 @@ module Dry
|
|
116
130
|
# Subscribe to an event
|
117
131
|
#
|
118
132
|
# @param [Symbol,String] event_id The event identifier
|
119
|
-
# @param [Hash]
|
133
|
+
# @param [Hash] filter_hash An optional filter for conditional listeners
|
120
134
|
#
|
121
135
|
# @return [Class] publisher class
|
122
136
|
#
|
123
137
|
# @api public
|
124
|
-
def subscribe(event_id,
|
125
|
-
listeners[event_id] << [block,
|
138
|
+
def subscribe(event_id, filter_hash = EMPTY_HASH, &block)
|
139
|
+
listeners[event_id] << [block, Filter.new(filter_hash)]
|
126
140
|
self
|
127
141
|
end
|
128
142
|
|
@@ -180,21 +194,28 @@ module Dry
|
|
180
194
|
|
181
195
|
# Subscribe to events.
|
182
196
|
#
|
183
|
-
# If the
|
197
|
+
# If the filter parameter is provided, filters events by payload.
|
184
198
|
#
|
185
199
|
# @param [Symbol,String,Object] object_or_event_id The event identifier or a listener object
|
186
|
-
# @param [Hash]
|
200
|
+
# @param [Hash] filter_hash An optional event filter
|
187
201
|
#
|
188
202
|
# @return [Object] self
|
189
203
|
#
|
190
204
|
# @api public
|
191
|
-
def subscribe(object_or_event_id,
|
192
|
-
if
|
193
|
-
|
205
|
+
def subscribe(object_or_event_id, filter_hash = EMPTY_HASH, &block)
|
206
|
+
if __bus__.can_handle?(object_or_event_id)
|
207
|
+
filter = Filter.new(filter_hash)
|
208
|
+
|
209
|
+
if block
|
210
|
+
__bus__.subscribe(object_or_event_id, filter, &block)
|
211
|
+
else
|
212
|
+
__bus__.attach(object_or_event_id, filter)
|
213
|
+
end
|
214
|
+
|
215
|
+
self
|
194
216
|
else
|
195
|
-
|
217
|
+
raise InvalidSubscriberError, object_or_event_id
|
196
218
|
end
|
197
|
-
self
|
198
219
|
end
|
199
220
|
|
200
221
|
# Unsubscribe a listener
|
@@ -217,7 +238,7 @@ module Dry
|
|
217
238
|
|
218
239
|
# Utility method which yields event with each of its listeners
|
219
240
|
#
|
220
|
-
# Listeners are already filtered out when
|
241
|
+
# Listeners are already filtered out when filter was provided during
|
221
242
|
# subscription
|
222
243
|
#
|
223
244
|
# @param [Symbol,String] event_id The event identifier
|
data/lib/dry/events/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -6,7 +6,8 @@ if RUBY_ENGINE == 'ruby' && ENV['COVERAGE'] == 'true'
|
|
6
6
|
end
|
7
7
|
|
8
8
|
begin
|
9
|
-
require '
|
9
|
+
require 'pry'
|
10
|
+
require 'pry-byebug'
|
10
11
|
rescue LoadError; end
|
11
12
|
|
12
13
|
require 'dry-events'
|
@@ -17,7 +18,9 @@ Dir[SPEC_ROOT.join('shared/**/*.rb')].each(&method(:require))
|
|
17
18
|
Dir[SPEC_ROOT.join('support/**/*.rb')].each(&method(:require))
|
18
19
|
|
19
20
|
RSpec.configure do |config|
|
21
|
+
config.warnings = true
|
20
22
|
config.disable_monkey_patching!
|
23
|
+
config.filter_run_when_matching :focus
|
21
24
|
|
22
25
|
config.after(:example) do
|
23
26
|
Dry::Events::Publisher.instance_variable_set(:@__registry__, Concurrent::Map.new)
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'dry/events/event'
|
2
|
+
|
3
|
+
RSpec.describe Dry::Events::Event do
|
4
|
+
subject(:event) do
|
5
|
+
described_class.new(event_id, payload)
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:payload) { {} }
|
9
|
+
|
10
|
+
describe 'invalid event name' do
|
11
|
+
let(:event_id) { nil }
|
12
|
+
it 'raises InvalidEventNameError' do
|
13
|
+
expect{ event }.to raise_error(Dry::Events::Event::InvalidEventNameError)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#[]' do
|
18
|
+
let(:event_id) { :test }
|
19
|
+
let(:payload) { {test: :foo} }
|
20
|
+
|
21
|
+
it 'fetches payload key' do
|
22
|
+
expect(event[:test]).to eq :foo
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'raises KeyError when no key found' do
|
26
|
+
expect { event[:fake] }.to raise_error(KeyError)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#payload' do
|
31
|
+
let(:event_id) { :test }
|
32
|
+
let(:payload) { {test: :foo} }
|
33
|
+
|
34
|
+
it 'returns payload if no argument' do
|
35
|
+
expect(event.payload).to eq payload
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'returns new event with payload payload' do
|
39
|
+
new_event = event.payload({ bar: :baz })
|
40
|
+
expect(new_event).to_not eq event
|
41
|
+
expect(new_event.payload).to eq ({test: :foo, bar: :baz})
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#to_h' do
|
46
|
+
let(:event_id) { :test }
|
47
|
+
let(:payload) { {test: :foo} }
|
48
|
+
|
49
|
+
it 'returns payload' do
|
50
|
+
expect(event.to_h).to eq payload
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#listener_method' do
|
55
|
+
let(:event_id) { :test }
|
56
|
+
|
57
|
+
it 'returns listener method name' do
|
58
|
+
expect(event.listener_method).to eq :on_test
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'replaces dots for underscores' do
|
62
|
+
ev = Dry::Events::Event.new('hello.world')
|
63
|
+
expect(ev.listener_method).to eq :on_hello_world
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
RSpec.describe Dry::Events::Filter do
|
2
|
+
subject(:filter) { described_class.new(query) }
|
3
|
+
|
4
|
+
context 'nested hash' do
|
5
|
+
let(:query) do
|
6
|
+
{ logger: { level: :info } }
|
7
|
+
end
|
8
|
+
|
9
|
+
specify do
|
10
|
+
expect(filter.()).to be false
|
11
|
+
expect(filter.(logger: { level: :info, output: :stdin })).to be true
|
12
|
+
expect(filter.(logger: { level: :debug })).to be false
|
13
|
+
expect(filter.(logger: :debug)).to be false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'multi-value check' do
|
18
|
+
let(:query) do
|
19
|
+
{ logger: { level: :info, output: :stdin } }
|
20
|
+
end
|
21
|
+
|
22
|
+
specify do
|
23
|
+
expect(filter.()).to be false
|
24
|
+
expect(filter.(logger: { level: :info, output: :stdin })).to be true
|
25
|
+
expect(filter.(logger: { level: :info })).to be false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'top-level array' do
|
30
|
+
let(:query) { %i(error fatal) }
|
31
|
+
|
32
|
+
specify do
|
33
|
+
expect(filter.()).to be false
|
34
|
+
expect(filter.(random: :hash)).to be false
|
35
|
+
expect(filter.(:error)).to be true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'nested array' do
|
40
|
+
let(:query) do
|
41
|
+
{ logger: { level: %i(info warn error fatal) } }
|
42
|
+
end
|
43
|
+
|
44
|
+
specify do
|
45
|
+
expect(filter.()).to be false
|
46
|
+
expect(filter.(logger: { level: :info, output: :stdin })).to be true
|
47
|
+
expect(filter.(logger: { level: :fatal })).to be true
|
48
|
+
expect(filter.(logger: { level: :debug })).to be false
|
49
|
+
expect(filter.(level: :debug)).to be false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'nested proc' do
|
54
|
+
let(:query) do
|
55
|
+
{ logger: { level: -> level { %i(error fatal).include?(level) } } }
|
56
|
+
end
|
57
|
+
|
58
|
+
specify do
|
59
|
+
expect(filter.()).to be false
|
60
|
+
expect(filter.(logger: { level: :error, output: :stdin })).to be true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'top-level proc' do
|
65
|
+
let(:query) do
|
66
|
+
-> level: :debug, ** { %i(error fatal).include?(level) }
|
67
|
+
end
|
68
|
+
|
69
|
+
specify do
|
70
|
+
expect(filter.()).to be false
|
71
|
+
expect(filter.(level: :error, output: :stdin)).to be true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -16,16 +16,30 @@ RSpec.describe Dry::Events::Listener do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe '.subscribe' do
|
19
|
-
|
20
|
-
result = []
|
19
|
+
let(:captured) { [] }
|
21
20
|
|
21
|
+
it 'subscribes a listener at class level' do
|
22
22
|
listener.subscribe(:test_event) do |event|
|
23
|
-
|
23
|
+
captured << event.id
|
24
24
|
end
|
25
25
|
|
26
26
|
publisher.publish(:test_event)
|
27
27
|
|
28
|
-
expect(
|
28
|
+
expect(captured).to eql([:test_event])
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'filters' do
|
32
|
+
it 'filters events' do
|
33
|
+
listener.subscribe(:test_event, level: :info) do |event|
|
34
|
+
captured << event.payload
|
35
|
+
end
|
36
|
+
|
37
|
+
publisher.publish(:test_event)
|
38
|
+
publisher.publish(:test_event, level: :debug)
|
39
|
+
publisher.publish(:test_event, level: :info)
|
40
|
+
|
41
|
+
expect(captured).to eql([level: :info])
|
42
|
+
end
|
29
43
|
end
|
30
44
|
end
|
31
45
|
end
|
@@ -39,6 +39,14 @@ RSpec.describe Dry::Events::Publisher do
|
|
39
39
|
|
40
40
|
expect(publisher.subscribed?(listener)).to be(true)
|
41
41
|
end
|
42
|
+
|
43
|
+
it 'raises an exception when subscribing to an unregister event' do
|
44
|
+
listener = -> * { }
|
45
|
+
|
46
|
+
expect {
|
47
|
+
publisher.subscribe(:not_register, &listener)
|
48
|
+
}.to raise_error(Dry::Events::InvalidSubscriberError, /not_register/)
|
49
|
+
end
|
42
50
|
end
|
43
51
|
|
44
52
|
describe '#register_event' do
|
@@ -83,6 +91,20 @@ RSpec.describe Dry::Events::Publisher do
|
|
83
91
|
|
84
92
|
expect(listener.captured).to eql(['it works'])
|
85
93
|
end
|
94
|
+
|
95
|
+
it 'raises an exception when subscribing with no methods to execute' do
|
96
|
+
listener = Object.new
|
97
|
+
|
98
|
+
expect {
|
99
|
+
publisher.subscribe(listener)
|
100
|
+
}.to raise_error(Dry::Events::InvalidSubscriberError, /never be executed/)
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'does not raise an exception when subscriber has methods for notification' do
|
104
|
+
listener = Object.new
|
105
|
+
def listener.on_test_event; nil; end
|
106
|
+
expect { publisher.subscribe(listener) }.not_to raise_error
|
107
|
+
end
|
86
108
|
end
|
87
109
|
|
88
110
|
describe '#publish' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-events
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -116,10 +116,13 @@ files:
|
|
116
116
|
- lib/dry/events/bus.rb
|
117
117
|
- lib/dry/events/constants.rb
|
118
118
|
- lib/dry/events/event.rb
|
119
|
+
- lib/dry/events/filter.rb
|
119
120
|
- lib/dry/events/listener.rb
|
120
121
|
- lib/dry/events/publisher.rb
|
121
122
|
- lib/dry/events/version.rb
|
122
123
|
- spec/spec_helper.rb
|
124
|
+
- spec/unit/dry/events/event_spec.rb
|
125
|
+
- spec/unit/dry/events/filter_spec.rb
|
123
126
|
- spec/unit/dry/events/listener_spec.rb
|
124
127
|
- spec/unit/dry/events/publisher_spec.rb
|
125
128
|
homepage: https://github.com/dry-rb/dry-events
|
@@ -141,12 +144,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
144
|
- !ruby/object:Gem::Version
|
142
145
|
version: '0'
|
143
146
|
requirements: []
|
144
|
-
|
145
|
-
rubygems_version: 2.7.3
|
147
|
+
rubygems_version: 3.0.3
|
146
148
|
signing_key:
|
147
149
|
specification_version: 4
|
148
150
|
summary: Pub/sub system
|
149
151
|
test_files:
|
150
152
|
- spec/spec_helper.rb
|
153
|
+
- spec/unit/dry/events/event_spec.rb
|
154
|
+
- spec/unit/dry/events/filter_spec.rb
|
151
155
|
- spec/unit/dry/events/listener_spec.rb
|
152
156
|
- spec/unit/dry/events/publisher_spec.rb
|