mediator-rb 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/Gemfile.lock +3 -1
- data/lib/mediator/{rb/version.rb → version.rb} +1 -3
- data/lib/mediator-rb.rb +1 -0
- data/lib/mediator.rb +51 -0
- data/mediator-rb.gemspec +3 -2
- data/sig/mediator/.gitignore +0 -0
- metadata +20 -5
- data/lib/mediator/rb.rb +0 -10
- data/sig/mediator/rb.rbs +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5d8253174b40e2f587c18e5ca41341366468d7a60c29d9f8f02eccc4dd40911
|
4
|
+
data.tar.gz: ce3779e1c27799685445997a34ecf2db9cfce8a4f4223c3ee25d9a74807ca52d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b815cf49e6aa431e090789a804a3ce66b0fb9236560c83f7e599503b4e0ce20eec2b724edd4153878e4539c2807003071a9a5283301d8d7f0c51f10e64096e47
|
7
|
+
data.tar.gz: 3bb3764fe14444551f833912c4b29d95c00ceee2ae359bdee17acbc070522557cae602c8cf6c530812c5c3c7f10e855783cff5f5df76c1427c60bd92442476f0
|
data/.rspec
CHANGED
data/Gemfile.lock
CHANGED
data/lib/mediator-rb.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "mediator"
|
data/lib/mediator.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'concurrent/map'
|
2
|
+
|
3
|
+
module Mediator
|
4
|
+
def self.init
|
5
|
+
Instance.new
|
6
|
+
end
|
7
|
+
|
8
|
+
class Instance
|
9
|
+
MultipleHandlers = Class.new(StandardError)
|
10
|
+
UnregisteredHandler = Class.new(StandardError)
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@handlers = Concurrent::Map.new
|
14
|
+
@listeners = Concurrent::Map.new { |h, k| h.compute_if_absent(k) { [] } }
|
15
|
+
end
|
16
|
+
|
17
|
+
def handle(klass, handler)
|
18
|
+
raise MultipleHandlers, "Multiple handlers not allowed for #{klass}" if handlers[klass]
|
19
|
+
|
20
|
+
handlers[klass] = handler
|
21
|
+
end
|
22
|
+
|
23
|
+
def subscribe(klass, handler)
|
24
|
+
listeners[klass] << handler
|
25
|
+
end
|
26
|
+
|
27
|
+
def accept(request)
|
28
|
+
handler!(request).then do |handler|
|
29
|
+
handler.call(request)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def publish(event)
|
34
|
+
listeners!(event).each do |listener|
|
35
|
+
listener.call(event)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
attr_reader :handlers, :listeners
|
42
|
+
|
43
|
+
def listeners!(event)
|
44
|
+
listeners.fetch(event.class) { raise UnregisteredHandler, "Missing listener for #{event.class}" }
|
45
|
+
end
|
46
|
+
|
47
|
+
def handler!(request)
|
48
|
+
handlers.fetch(request.class) { raise UnregisteredHandler, "Missing handler for #{request.class}" }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/mediator-rb.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "lib/mediator/
|
3
|
+
require_relative "lib/mediator/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "mediator-rb"
|
7
|
-
spec.version = Mediator::
|
7
|
+
spec.version = Mediator::VERSION
|
8
8
|
spec.authors = ["Laertis Pappas"]
|
9
9
|
spec.email = ["laertis.pappas@gmail.com"]
|
10
10
|
|
@@ -30,5 +30,6 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.executables = []
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
|
+
spec.add_dependency "concurrent-ruby"
|
33
34
|
spec.add_development_dependency "rspec"
|
34
35
|
end
|
File without changes
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mediator-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Laertis Pappas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: concurrent-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,10 +52,11 @@ files:
|
|
38
52
|
- LICENSE.txt
|
39
53
|
- README.md
|
40
54
|
- Rakefile
|
41
|
-
- lib/mediator
|
42
|
-
- lib/mediator
|
55
|
+
- lib/mediator-rb.rb
|
56
|
+
- lib/mediator.rb
|
57
|
+
- lib/mediator/version.rb
|
43
58
|
- mediator-rb.gemspec
|
44
|
-
- sig/mediator
|
59
|
+
- sig/mediator/.gitignore
|
45
60
|
homepage: https://www.github.com/laertispappas/mediator-rb
|
46
61
|
licenses:
|
47
62
|
- MIT
|
data/lib/mediator/rb.rb
DELETED