dry-events 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f18471031f2c5dcd9d2ea488ab48f5d42d9bd44de9158241dbb43324dd6fc0b4
4
- data.tar.gz: '01160358743e52191c19bfc8b3a0550b4a201967f5fe96a8c60dfe50b560b2e1'
3
+ metadata.gz: 1423fe7cb9ad77d3af4fea999666f1ff275204e5dad84e4ba8f83ea230b0e2d0
4
+ data.tar.gz: 30a0f8b923a890af19b9937fbf502b51c8a568b33444bba0f3d09995045a016d
5
5
  SHA512:
6
- metadata.gz: d5d2e2a9920e5fe2241d5ae0cf42337169f4836353475aeb1bf4d8e621eac86b33143440a160ba5addf0b3cae548772f02c1b42bd86d88283e466835714f42bd
7
- data.tar.gz: 6b4b8cdc169c69523dc8ba29362113d4fd7b127d3532f80572bf687c44b07c02fbe95788d2356a1316b363e54fab1ad8db3cd4801823aecce0f59926f9ec5e9b
6
+ metadata.gz: d985af03635dea492d89ae37dd5935aa4576d92ca2201bbf4096d12a396df7857fd0007bf7a28275f7d5a985560ef26532ca977bf1ea05e04e815d88db02bd8f
7
+ data.tar.gz: 8de1e80e33a2a5429fddce3a675ed9c72ae5224bc919d6fcab5300f4f8d7b11617d18330b0af5451884e76accbc755b37d4282155c0aa52dbddd380e1cc723af
@@ -2,25 +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.5.0
10
- - 2.4.3
11
- - 2.3.6
12
- - jruby-9.1.9.0
8
+ - 2.6.3
9
+ - 2.5.5
10
+ - 2.4.6
11
+ - jruby-9.2.7.0
13
12
  - truffleruby
14
13
  matrix:
15
14
  allow_failures:
16
15
  - rvm: truffleruby
16
+ - rvm: jruby-9.2.7.0
17
17
  env:
18
18
  global:
19
19
  - COVERAGE='true'
20
20
  notifications:
21
21
  webhooks:
22
- urls:
23
- - https://webhooks.gitter.im/e/19098b4253a72c9796db
24
22
  on_success: change
25
23
  on_failure: always
26
24
  on_start: false
@@ -1,3 +1,11 @@
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
+
1
9
  # v0.1.1 2019-03-22
2
10
 
3
11
  ## Added
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  [gem]: https://rubygems.org/gems/dry-events
2
- [travis]: https://travis-ci.org/dry-rb/dry-events
2
+ [travis]: https://travis-ci.com/dry-rb/dry-events
3
3
  [codeclimate]: https://codeclimate.com/github/dry-rb/dry-events
4
- [coveralls]: https://coveralls.io/r/dry-rb/dry-events
4
+ [chat]: https://dry-rb.zulipchat.com
5
5
  [inchpages]: http://inch-ci.org/github/dry-rb/dry-events
6
6
 
7
- # dry-events [![Join the chat at https://gitter.im/dry-rb/chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dry-rb/chat)
7
+ # dry-events [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
8
8
 
9
9
  [![Gem Version](https://badge.fury.io/rb/dry-events.svg)][gem]
10
- [![Build Status](https://travis-ci.org/dry-rb/dry-events.svg?branch=master)][travis]
10
+ [![Build Status](https://travis-ci.com/dry-rb/dry-events.svg?branch=master)][travis]
11
11
  [![Code Climate](https://codeclimate.com/github/dry-rb/dry-events/badges/gpa.svg)][codeclimate]
12
12
  [![Test Coverage](https://codeclimate.com/github/dry-rb/dry-events/badges/coverage.svg)][codeclimate]
13
13
  [![Inline docs](http://inch-ci.org/github/dry-rb/dry-events.svg?branch=master)][inchpages]
@@ -76,6 +76,19 @@ module Dry
76
76
  def subscribed?(listener)
77
77
  listeners.values.any? { |value| value.any? { |block, _| block.equal?(listener) } }
78
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
91
+ end
79
92
  end
80
93
  end
81
94
  end
@@ -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 Optional 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 = EMPTY_HASH)
41
+ def initialize(id, payload)
28
42
  @id = id
29
43
  @payload = payload
30
44
  end
@@ -19,6 +19,19 @@ module Dry
19
19
  end
20
20
  end
21
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
+
22
35
  # Extension used for classes that can publish events
23
36
  #
24
37
  # @example
@@ -190,14 +203,19 @@ module Dry
190
203
  #
191
204
  # @api public
192
205
  def subscribe(object_or_event_id, filter_hash = EMPTY_HASH, &block)
193
- filter = Filter.new(filter_hash)
206
+ if __bus__.can_handle?(object_or_event_id)
207
+ filter = Filter.new(filter_hash)
194
208
 
195
- if block
196
- __bus__.subscribe(object_or_event_id, filter, &block)
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
197
216
  else
198
- __bus__.attach(object_or_event_id, filter)
217
+ raise InvalidSubscriberError, object_or_event_id
199
218
  end
200
- self
201
219
  end
202
220
 
203
221
  # Unsubscribe a listener
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Events
3
- VERSION = '0.1.1'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -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
@@ -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.1.1
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: 2019-03-22 00:00:00.000000000 Z
11
+ date: 2019-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -121,6 +121,7 @@ files:
121
121
  - lib/dry/events/publisher.rb
122
122
  - lib/dry/events/version.rb
123
123
  - spec/spec_helper.rb
124
+ - spec/unit/dry/events/event_spec.rb
124
125
  - spec/unit/dry/events/filter_spec.rb
125
126
  - spec/unit/dry/events/listener_spec.rb
126
127
  - spec/unit/dry/events/publisher_spec.rb
@@ -143,12 +144,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
144
  - !ruby/object:Gem::Version
144
145
  version: '0'
145
146
  requirements: []
146
- rubygems_version: 3.0.1
147
+ rubygems_version: 3.0.3
147
148
  signing_key:
148
149
  specification_version: 4
149
150
  summary: Pub/sub system
150
151
  test_files:
151
152
  - spec/spec_helper.rb
153
+ - spec/unit/dry/events/event_spec.rb
152
154
  - spec/unit/dry/events/filter_spec.rb
153
155
  - spec/unit/dry/events/listener_spec.rb
154
156
  - spec/unit/dry/events/publisher_spec.rb