dry-events 0.2.0 → 0.3.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: 1423fe7cb9ad77d3af4fea999666f1ff275204e5dad84e4ba8f83ea230b0e2d0
4
- data.tar.gz: 30a0f8b923a890af19b9937fbf502b51c8a568b33444bba0f3d09995045a016d
3
+ metadata.gz: e9092f7b60a8ad71e376a074367328c96be1ca31c278dfee8a7f8e8a6c11d02d
4
+ data.tar.gz: fb9e14c0df38ac76a64fd9811367322bccee5465d259a28537de902b292a6c72
5
5
  SHA512:
6
- metadata.gz: d985af03635dea492d89ae37dd5935aa4576d92ca2201bbf4096d12a396df7857fd0007bf7a28275f7d5a985560ef26532ca977bf1ea05e04e815d88db02bd8f
7
- data.tar.gz: 8de1e80e33a2a5429fddce3a675ed9c72ae5224bc919d6fcab5300f4f8d7b11617d18330b0af5451884e76accbc755b37d4282155c0aa52dbddd380e1cc723af
6
+ metadata.gz: 995a2c14a459edd0e0fcb05f19629829581fce7e14e7edbbb8c9fe2e555769b3cd15a89b55d75826da0d49a70b7a818de802b7f92d496f0012bf675c61d3bcc1
7
+ data.tar.gz: db26c95473237a3f783f06ffc71bad0351ab24a4922a544d227c586f9028d49655f300c038f63697d8fbe297e774b77692e52752167b12ae88660f69d6a8d344
data/CHANGELOG.md CHANGED
@@ -1,14 +1,32 @@
1
- # v0.2.0 2019-07-24
1
+ <!--- DO NOT EDIT THIS FILE - IT'S AUTOMATICALLY GENERATED VIA DEVTOOLS --->
2
2
 
3
- ## Fixed
3
+ ## 0.3.0 2021-04-11
4
+
5
+
6
+ ### Fixed
7
+
8
+ - Trying to publish an unregistered event raises a meaningful error now (issue #13 fixed via #14) (@landongrindheim)
9
+
10
+ ### Changed
11
+
12
+ - Dependency on dry-equalizer was changed to dry-core (@solnic)
13
+
14
+ [Compare v0.2.0...v0.3.0](https://github.com/dry-rb/dry-events/compare/v0.2.0...v0.3.0)
15
+
16
+ ## 0.2.0 2019-07-24
17
+
18
+
19
+ ### Fixed
20
+
21
+ - Trying to subscribe to a non-existant event raises an exception (issue #3) (@GustavoCaso + @mensfeld)
4
22
 
5
- * Trying to subscribe to a non-existant event raises an exception (issue #3) (@GustavoCaso + @mensfeld)
6
23
 
7
24
  [Compare v0.1.1...v0.2.0](https://github.com/dry-rb/dry-events/compare/v0.1.1...v0.2.0)
8
25
 
9
- # v0.1.1 2019-03-22
26
+ ## 0.1.1 2019-03-22
27
+
10
28
 
11
- ## Added
29
+ ### Added
12
30
 
13
31
  - Subscription filters can be more complex: nested hash inclusion, array inclusion, and proc checks were added (flash-gordon)
14
32
  ```ruby
@@ -34,8 +52,9 @@
34
52
  subscribe(:event, logger: { level: -> level { %i(info warn error).include?(level) })
35
53
  ```
36
54
 
55
+
37
56
  [Compare v0.1.0...v0.1.1](https://github.com/dry-rb/dry-events/compare/v0.1.0...v0.1.1)
38
57
 
39
- # v0.1.0 2018-01-02
58
+ ## 0.1.0 2018-01-02
40
59
 
41
60
  First public release
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 dry-rb team
3
+ Copyright (c) 2015-2021 dry-rb team
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
data/README.md CHANGED
@@ -1,24 +1,29 @@
1
1
  [gem]: https://rubygems.org/gems/dry-events
2
- [travis]: https://travis-ci.com/dry-rb/dry-events
3
- [codeclimate]: https://codeclimate.com/github/dry-rb/dry-events
2
+ [actions]: https://github.com/dry-rb/dry-events/actions
3
+ [codacy]: https://www.codacy.com/gh/dry-rb/dry-events
4
4
  [chat]: https://dry-rb.zulipchat.com
5
5
  [inchpages]: http://inch-ci.org/github/dry-rb/dry-events
6
6
 
7
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.com/dry-rb/dry-events.svg?branch=master)][travis]
11
- [![Code Climate](https://codeclimate.com/github/dry-rb/dry-events/badges/gpa.svg)][codeclimate]
12
- [![Test Coverage](https://codeclimate.com/github/dry-rb/dry-events/badges/coverage.svg)][codeclimate]
10
+ [![CI Status](https://github.com/dry-rb/dry-events/workflows/ci/badge.svg)][actions]
11
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/3170162485134ea3b778c7e20e180b26)][codacy]
12
+ [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/3170162485134ea3b778c7e20e180b26)][codacy]
13
13
  [![Inline docs](http://inch-ci.org/github/dry-rb/dry-events.svg?branch=master)][inchpages]
14
14
 
15
- Standalone pub/sub system.
16
-
17
15
  ## Links
18
16
 
19
- * [User docs](http://dry-rb.org/gems/dry-events)
20
- * [API docs](http://rubydoc.info/gems/dry-events)
17
+ * [User documentation](http://dry-rb.org/gems/dry-events)
18
+ * [API documentation](http://rubydoc.info/gems/dry-events)
19
+
20
+ ## Supported Ruby versions
21
+
22
+ This library officially supports the following Ruby versions:
23
+
24
+ * MRI >= `2.5`
25
+ * jruby >= `9.2`
21
26
 
22
- ## LICENSE
27
+ ## License
23
28
 
24
29
  See `LICENSE` file.
data/dry-events.gemspec CHANGED
@@ -1,25 +1,37 @@
1
- # coding: utf-8
2
- require File.expand_path('../lib/dry/events/version', __FILE__)
1
+ # frozen_string_literal: true
2
+ # this file is managed by dry-rb/devtools project
3
+
4
+ lib = File.expand_path('lib', __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'dry/events/version'
3
7
 
4
8
  Gem::Specification.new do |spec|
5
9
  spec.name = 'dry-events'
6
- spec.version = Dry::Events::VERSION
7
- spec.authors = ['Piotr Solnica']
8
- spec.email = ['piotr.solnica+oss@gmail.com']
9
- spec.summary = 'Pub/sub system'
10
- spec.homepage = 'https://github.com/dry-rb/dry-events'
10
+ spec.authors = ["Piotr Solnica"]
11
+ spec.email = ["piotr.solnica+oss@gmail.com"]
11
12
  spec.license = 'MIT'
13
+ spec.version = Dry::Events::VERSION.dup
12
14
 
13
- spec.files = `git ls-files -z`.split("\x0")
14
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.summary = "Pub/sub system"
16
+ spec.description = spec.summary
17
+ spec.homepage = 'https://dry-rb.org/gems/dry-events'
18
+ spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-events.gemspec", "lib/**/*"]
19
+ spec.bindir = 'bin'
20
+ spec.executables = []
16
21
  spec.require_paths = ['lib']
17
22
 
18
- spec.add_runtime_dependency 'concurrent-ruby', '~> 1.0'
19
- spec.add_runtime_dependency 'dry-core', '~> 0.4'
20
- spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
23
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
24
+ spec.metadata['changelog_uri'] = 'https://github.com/dry-rb/dry-events/blob/master/CHANGELOG.md'
25
+ spec.metadata['source_code_uri'] = 'https://github.com/dry-rb/dry-events'
26
+ spec.metadata['bug_tracker_uri'] = 'https://github.com/dry-rb/dry-events/issues'
27
+
28
+ spec.required_ruby_version = ">= 2.5.0"
29
+
30
+ # to update dependencies edit project.yml
31
+ spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
32
+ spec.add_runtime_dependency "dry-core", "~> 0.5", ">= 0.5"
21
33
 
22
- spec.add_development_dependency 'bundler'
23
- spec.add_development_dependency 'rake'
24
- spec.add_development_dependency 'rspec'
34
+ spec.add_development_dependency "bundler"
35
+ spec.add_development_dependency "rake"
36
+ spec.add_development_dependency "rspec"
25
37
  end
data/lib/dry-events.rb CHANGED
@@ -1 +1,3 @@
1
- require 'dry/events'
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/events"
data/lib/dry/events.rb CHANGED
@@ -1 +1,3 @@
1
- require 'dry/events/publisher'
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/events/publisher"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'dry/events/constants'
2
4
 
3
5
  module Dry
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'concurrent/map'
2
4
  require 'dry/core/constants'
3
5
 
@@ -1,4 +1,6 @@
1
- require 'dry/equalizer'
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/core/equalizer'
2
4
  require 'dry/events/constants'
3
5
 
4
6
  module Dry
@@ -12,7 +14,7 @@ module Dry
12
14
  InvalidEventNameError = Class.new(StandardError) do
13
15
  # @api private
14
16
  def initialize
15
- super("please provide a valid event name, it could be either String or Symbol")
17
+ super('please provide a valid event name, it could be either String or Symbol')
16
18
  end
17
19
  end
18
20
 
@@ -87,4 +89,3 @@ module Dry
87
89
  end
88
90
  end
89
91
  end
90
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'set'
2
4
 
3
5
  module Dry
@@ -1,4 +1,6 @@
1
- require 'dry/equalizer'
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/core/equalizer'
2
4
  require 'dry/events/publisher'
3
5
 
4
6
  module Dry
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'concurrent/map'
2
4
 
3
5
  require 'dry/core/class_attributes'
@@ -27,7 +29,18 @@ module Dry
27
29
  when String, Symbol
28
30
  super("you are trying to subscribe to an event: `#{object_or_event_id}` that has not been registered")
29
31
  else
30
- super("you try use subscriber object that will never be executed")
32
+ super('you try use subscriber object that will never be executed')
33
+ end
34
+ end
35
+ end
36
+
37
+ UnregisteredEventError = Class.new(StandardError) do
38
+ def initialize(object_or_event_id)
39
+ case object_or_event_id
40
+ when String, Symbol
41
+ super("You are trying to publish an unregistered event: `#{object_or_event_id}`")
42
+ else
43
+ super("You are trying to publish an unregistered event")
31
44
  end
32
45
  end
33
46
  end
@@ -90,6 +103,7 @@ module Dry
90
103
  # @api public
91
104
  def self.[](id)
92
105
  raise PublisherAlreadyRegisteredError.new(id) if registry.key?(id)
106
+
93
107
  new(id)
94
108
  end
95
109
 
@@ -180,6 +194,7 @@ module Dry
180
194
  __bus__.events[event_id] = Event.new(event_id, payload)
181
195
  self
182
196
  end
197
+
183
198
  # Publish an event
184
199
  #
185
200
  # @param [String] event_id The event identifier
@@ -187,8 +202,12 @@ module Dry
187
202
  #
188
203
  # @api public
189
204
  def publish(event_id, payload = EMPTY_HASH)
190
- __bus__.publish(event_id, payload)
191
- self
205
+ if __bus__.can_handle?(event_id)
206
+ __bus__.publish(event_id, payload)
207
+ self
208
+ else
209
+ raise UnregisteredEventError, event_id
210
+ end
192
211
  end
193
212
  alias_method :trigger, :publish
194
213
 
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Dry
2
4
  module Events
3
- VERSION = '0.2.0'.freeze
5
+ VERSION = '0.3.0'
4
6
  end
5
7
  end
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.2.0
4
+ version: 0.3.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-07-24 00:00:00.000000000 Z
11
+ date: 2021-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -30,28 +30,20 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.4'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0.4'
41
- - !ruby/object:Gem::Dependency
42
- name: dry-equalizer
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
33
+ version: '0.5'
34
+ - - ">="
46
35
  - !ruby/object:Gem::Version
47
- version: '0.2'
36
+ version: '0.5'
48
37
  type: :runtime
49
38
  prerelease: false
50
39
  version_requirements: !ruby/object:Gem::Requirement
51
40
  requirements:
52
41
  - - "~>"
53
42
  - !ruby/object:Gem::Version
54
- version: '0.2'
43
+ version: '0.5'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0.5'
55
47
  - !ruby/object:Gem::Dependency
56
48
  name: bundler
57
49
  requirement: !ruby/object:Gem::Requirement
@@ -94,22 +86,16 @@ dependencies:
94
86
  - - ">="
95
87
  - !ruby/object:Gem::Version
96
88
  version: '0'
97
- description:
89
+ description: Pub/sub system
98
90
  email:
99
91
  - piotr.solnica+oss@gmail.com
100
92
  executables: []
101
93
  extensions: []
102
94
  extra_rdoc_files: []
103
95
  files:
104
- - ".gitignore"
105
- - ".rspec"
106
- - ".travis.yml"
107
96
  - CHANGELOG.md
108
- - CONTRIBUTING.md
109
- - Gemfile
110
97
  - LICENSE
111
98
  - README.md
112
- - Rakefile
113
99
  - dry-events.gemspec
114
100
  - lib/dry-events.rb
115
101
  - lib/dry/events.rb
@@ -120,15 +106,14 @@ files:
120
106
  - lib/dry/events/listener.rb
121
107
  - lib/dry/events/publisher.rb
122
108
  - lib/dry/events/version.rb
123
- - spec/spec_helper.rb
124
- - spec/unit/dry/events/event_spec.rb
125
- - spec/unit/dry/events/filter_spec.rb
126
- - spec/unit/dry/events/listener_spec.rb
127
- - spec/unit/dry/events/publisher_spec.rb
128
- homepage: https://github.com/dry-rb/dry-events
109
+ homepage: https://dry-rb.org/gems/dry-events
129
110
  licenses:
130
111
  - MIT
131
- metadata: {}
112
+ metadata:
113
+ allowed_push_host: https://rubygems.org
114
+ changelog_uri: https://github.com/dry-rb/dry-events/blob/master/CHANGELOG.md
115
+ source_code_uri: https://github.com/dry-rb/dry-events
116
+ bug_tracker_uri: https://github.com/dry-rb/dry-events/issues
132
117
  post_install_message:
133
118
  rdoc_options: []
134
119
  require_paths:
@@ -137,20 +122,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
122
  requirements:
138
123
  - - ">="
139
124
  - !ruby/object:Gem::Version
140
- version: '0'
125
+ version: 2.5.0
141
126
  required_rubygems_version: !ruby/object:Gem::Requirement
142
127
  requirements:
143
128
  - - ">="
144
129
  - !ruby/object:Gem::Version
145
130
  version: '0'
146
131
  requirements: []
147
- rubygems_version: 3.0.3
132
+ rubygems_version: 3.1.6
148
133
  signing_key:
149
134
  specification_version: 4
150
135
  summary: Pub/sub system
151
- test_files:
152
- - spec/spec_helper.rb
153
- - spec/unit/dry/events/event_spec.rb
154
- - spec/unit/dry/events/filter_spec.rb
155
- - spec/unit/dry/events/listener_spec.rb
156
- - spec/unit/dry/events/publisher_spec.rb
136
+ test_files: []
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- .DS_Store
2
- coverage
3
- /.bundle
4
- vendor/bundle
5
- bin/
6
- tmp/
7
- .idea/
8
- Gemfile.lock
9
- spec/test_logs/*.log
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --color
2
- --require spec_helper
3
- --order random
data/.travis.yml DELETED
@@ -1,24 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
- bundler_args: --without benchmarks console tools
4
- script: "bundle exec rake ci"
5
- after_success:
6
- - '[ "${TRAVIS_JOB_NUMBER#*.}" = "1" ] && [ "$TRAVIS_BRANCH" = "master" ] && bundle exec codeclimate-test-reporter'
7
- rvm:
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
17
- env:
18
- global:
19
- - COVERAGE='true'
20
- notifications:
21
- webhooks:
22
- on_success: change
23
- on_failure: always
24
- on_start: false
data/CONTRIBUTING.md DELETED
@@ -1,29 +0,0 @@
1
- # Issue Guidelines
2
-
3
- ## Reporting bugs
4
-
5
- If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
6
-
7
- ## Reporting feature requests
8
-
9
- Report a feature request **only after discussing it first on [discuss.dry-rb.org](https://discuss.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
10
-
11
- ## Reporting questions, support requests, ideas, concerns etc.
12
-
13
- **PLEASE DON'T** - use [discuss.dry-rb.org](http://discuss.dry-rb.org) instead.
14
-
15
- # Pull Request Guidelines
16
-
17
- A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
18
-
19
- Other requirements:
20
-
21
- 1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
22
- 2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
23
- 3) Add API documentation if it's a new feature
24
- 4) Update API documentation if it changes an existing feature
25
- 5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
26
-
27
- # Asking for help
28
-
29
- If these guidelines aren't helpful, and you're stuck, please post a message on [discuss.dry-rb.org](https://discuss.dry-rb.org).
data/Gemfile DELETED
@@ -1,14 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- group :test do
6
- gem 'rack'
7
- gem 'simplecov', platform: :mri, require: false
8
- gem 'codeclimate-test-reporter', require: false
9
- end
10
-
11
- group :tools do
12
- gem 'pry'
13
- gem 'pry-byebug', platform: :mri
14
- end
data/Rakefile DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env rake
2
- require 'bundler/gem_tasks'
3
-
4
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
5
-
6
- require 'rspec/core'
7
- require 'rspec/core/rake_task'
8
-
9
- task default: :spec
10
-
11
- desc 'Run all specs in spec directory'
12
- RSpec::Core::RakeTask.new(:spec)
13
-
14
- desc "Run CI tasks"
15
- task ci: [:spec]
data/spec/spec_helper.rb DELETED
@@ -1,28 +0,0 @@
1
- if RUBY_ENGINE == 'ruby' && ENV['COVERAGE'] == 'true'
2
- require 'simplecov'
3
- SimpleCov.start do
4
- add_filter '/spec/'
5
- end
6
- end
7
-
8
- begin
9
- require 'pry'
10
- require 'pry-byebug'
11
- rescue LoadError; end
12
-
13
- require 'dry-events'
14
-
15
- SPEC_ROOT = Pathname(__dir__)
16
-
17
- Dir[SPEC_ROOT.join('shared/**/*.rb')].each(&method(:require))
18
- Dir[SPEC_ROOT.join('support/**/*.rb')].each(&method(:require))
19
-
20
- RSpec.configure do |config|
21
- config.warnings = true
22
- config.disable_monkey_patching!
23
- config.filter_run_when_matching :focus
24
-
25
- config.after(:example) do
26
- Dry::Events::Publisher.instance_variable_set(:@__registry__, Concurrent::Map.new)
27
- end
28
- end
@@ -1,66 +0,0 @@
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
@@ -1,74 +0,0 @@
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
@@ -1,45 +0,0 @@
1
- require 'dry/events/listener'
2
-
3
- RSpec.describe Dry::Events::Listener do
4
- subject(:listener) do
5
- Class.new {
6
- include Dry::Events::Listener[:test_publisher]
7
- }
8
- end
9
-
10
- let!(:publisher) do
11
- Class.new {
12
- include Dry::Events::Publisher[:test_publisher]
13
-
14
- register_event :test_event
15
- }.new
16
- end
17
-
18
- describe '.subscribe' do
19
- let(:captured) { [] }
20
-
21
- it 'subscribes a listener at class level' do
22
- listener.subscribe(:test_event) do |event|
23
- captured << event.id
24
- end
25
-
26
- publisher.publish(:test_event)
27
-
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
43
- end
44
- end
45
- end
@@ -1,147 +0,0 @@
1
- require 'dry/events/publisher'
2
-
3
- RSpec.describe Dry::Events::Publisher do
4
- subject(:publisher) do
5
- Class.new {
6
- include Dry::Events::Publisher[:test_publisher]
7
-
8
- register_event :test_event
9
- }.new
10
- end
11
-
12
- describe '.[]' do
13
- it 'creates a publisher extension with provided id' do
14
- publisher = Class.new do
15
- include Dry::Events::Publisher[:my_publisher]
16
- end
17
-
18
- expect(Dry::Events::Publisher.registry[:my_publisher]).to be(publisher)
19
- end
20
-
21
- it 'does not allow same id to be used for than once' do
22
- create_publisher = -> do
23
- Class.new do
24
- include Dry::Events::Publisher[:my_publisher]
25
- end
26
- end
27
-
28
- create_publisher.()
29
-
30
- expect { create_publisher.() }.to raise_error(Dry::Events::PublisherAlreadyRegisteredError, /my_publisher/)
31
- end
32
- end
33
-
34
- describe '.subscribe' do
35
- it 'subscribes a listener at class level' do
36
- listener = -> * { }
37
-
38
- publisher.class.subscribe(:test_event, &listener)
39
-
40
- expect(publisher.subscribed?(listener)).to be(true)
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
50
- end
51
-
52
- describe '#register_event' do
53
- it 'registers a new event at instance level' do
54
- listener = -> * { }
55
-
56
- publisher.register_event(:test_another_event).subscribe(:test_another_event, &listener)
57
-
58
- expect(publisher.subscribed?(listener)).to be(true)
59
- end
60
- end
61
-
62
- describe '#subscribe' do
63
- it 'subscribes a listener function' do
64
- listener = -> * { }
65
-
66
- publisher.subscribe(:test_event, &listener)
67
-
68
- expect(publisher.subscribed?(listener)).to be(true)
69
- end
70
-
71
- it 'subscribes a listener object' do
72
- listener = Class.new do
73
- attr_reader :captured
74
-
75
- def initialize
76
- @captured = []
77
- end
78
-
79
- def on_test_event(event)
80
- captured << event[:message]
81
- end
82
- end.new
83
-
84
- publisher.subscribe(listener).publish(:test_event, message: 'it works')
85
-
86
- expect(listener.captured).to eql(['it works'])
87
-
88
- publisher.unsubscribe(listener)
89
-
90
- publisher.publish(:test_event, message: 'it works')
91
-
92
- expect(listener.captured).to eql(['it works'])
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
108
- end
109
-
110
- describe '#publish' do
111
- it 'publishes an event' do
112
- result = []
113
- listener = -> event { result << event[:message] }
114
-
115
- publisher.subscribe(:test_event, &listener).publish(:test_event, message: 'it works')
116
-
117
- expect(result).to eql(['it works'])
118
- end
119
-
120
- it 'publishes an event filtered by a query' do
121
- result = []
122
- listener = -> test: { result << test }
123
-
124
- publisher.
125
- subscribe(:test_event, test: true, &listener).
126
- publish(:test_event, test: false).
127
- publish(:test_event, test: true)
128
-
129
- expect(result).to eql([true])
130
- end
131
- end
132
-
133
- describe '#process' do
134
- it 'yields event and its listeners' do
135
- result = []
136
- listener = -> event { result << event.id }
137
-
138
- publisher.subscribe(:test_event, &listener)
139
-
140
- publisher.process(:test_event) do |event, listener|
141
- listener.(event)
142
- end
143
-
144
- expect(result).to eql([:test_event])
145
- end
146
- end
147
- end