pub-sub 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pub_sub/configuration.rb +45 -0
- data/lib/pub_sub/container.rb +34 -0
- data/lib/pub_sub/hash.rb +39 -0
- data/lib/pub_sub/publisher.rb +18 -0
- data/lib/pub_sub/subscriber.rb +23 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c40743db194975eb5a6157d9818dda65ecadbddef131c9b4048d61a26567ae67
|
4
|
+
data.tar.gz: 98e1db527cc5a8ddc442026857922afa800949da0054b5e53f1e7c6dfc71d0a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1342d94d8e4333fe598cf6419183ae4528be0aa9e4fe6f9bf1faf058f3a0409169be9585e6b93048c2460290cf32c1c0b9f4cd9090dda68bdc8450d1fcd65c81
|
7
|
+
data.tar.gz: 0f1b05c15ac460f40413a8530f44c87a0d44b1b2b277596d506f05632fcc1f275e83bace387b66f527ac0ea502c2b57d08f9db1da370f3056bb1e6c776949a6c
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
require 'pub_sub/container'
|
5
|
+
require 'pub_sub/hash'
|
6
|
+
require 'pub_sub/publisher'
|
7
|
+
require 'pub_sub/subscriber'
|
8
|
+
|
9
|
+
module PubSub
|
10
|
+
class Configuration
|
11
|
+
private_class_method :new
|
12
|
+
attr_reader :container
|
13
|
+
|
14
|
+
@@singleton__instance = nil
|
15
|
+
@@singleton__mutex = Mutex.new
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def init(container = ::PubSub::Hash)
|
19
|
+
this(container)
|
20
|
+
end
|
21
|
+
|
22
|
+
def this(container = ::PubSub::Hash)
|
23
|
+
return @@singleton__instance if @@singleton__instance
|
24
|
+
|
25
|
+
@@singleton__mutex.synchronize do
|
26
|
+
return @@singleton__instance if @@singleton__instance
|
27
|
+
|
28
|
+
@@singleton__instance = new(container)
|
29
|
+
end
|
30
|
+
@@singleton__instance
|
31
|
+
end
|
32
|
+
|
33
|
+
def container
|
34
|
+
this.container
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def initialize(container)
|
41
|
+
@container = container.new
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PubSub
|
4
|
+
class Container
|
5
|
+
|
6
|
+
def pub(*)
|
7
|
+
raise NotImplementedError
|
8
|
+
end
|
9
|
+
|
10
|
+
def sub(*)
|
11
|
+
raise NotImplementedError
|
12
|
+
end
|
13
|
+
|
14
|
+
def unsub(*)
|
15
|
+
raise NotImplementedError
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def retrieve_klasses(iterable)
|
21
|
+
iterable.map{|clazz| clazz.split('::').inject(Object) {|o,c| o.const_get c}}
|
22
|
+
end
|
23
|
+
|
24
|
+
def fan_out(clazzes, args)
|
25
|
+
mutex = Mutex.new
|
26
|
+
|
27
|
+
mutex.synchronize do
|
28
|
+
clazzes.each{ |clazz| clazz.call(args) }
|
29
|
+
true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
data/lib/pub_sub/hash.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "request_store"
|
3
|
+
|
4
|
+
module PubSub
|
5
|
+
class Hash < Container
|
6
|
+
def initialize
|
7
|
+
RequestStore.store[:hash] = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def pub(event, args={})
|
11
|
+
iterable_subscriber_list = RequestStore.store[:hash][event]
|
12
|
+
return false unless iterable_subscriber_list
|
13
|
+
|
14
|
+
clazzes = retrieve_klasses(iterable_subscriber_list)
|
15
|
+
fan_out(clazzes, args)
|
16
|
+
end
|
17
|
+
|
18
|
+
def sub(event, klazz_name)
|
19
|
+
mutex = Mutex.new
|
20
|
+
|
21
|
+
mutex.synchronize do
|
22
|
+
RequestStore.store[:hash][event] = [] unless RequestStore.store[:hash][event]
|
23
|
+
RequestStore.store[:hash][event] << klazz_name unless RequestStore.store[:hash][event].include?(klazz_name)
|
24
|
+
end
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def unsub(event, klazz_name)
|
29
|
+
mutex = Mutex.new
|
30
|
+
|
31
|
+
mutex.synchronize do
|
32
|
+
return false unless RequestStore.store[:hash][event]
|
33
|
+
|
34
|
+
RequestStore.store[:hash][event].delete(klazz_name)
|
35
|
+
true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PubSub
|
4
|
+
module Publisher
|
5
|
+
Error = Class.new(StandardError)
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def pub(event, args={})
|
13
|
+
PubSub::Configuration.container.pub(event, args)
|
14
|
+
true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PubSub
|
4
|
+
module Subscriber
|
5
|
+
Error = Class.new(StandardError)
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def sub(event)
|
13
|
+
PubSub::Configuration.container.sub(event, self.name)
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def unsub(event)
|
18
|
+
PubSub::Configuration.container.unsub(event, self.name)
|
19
|
+
true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pub-sub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Saltykov
|
@@ -15,7 +15,12 @@ email:
|
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
|
-
files:
|
18
|
+
files:
|
19
|
+
- lib/pub_sub/configuration.rb
|
20
|
+
- lib/pub_sub/container.rb
|
21
|
+
- lib/pub_sub/hash.rb
|
22
|
+
- lib/pub_sub/publisher.rb
|
23
|
+
- lib/pub_sub/subscriber.rb
|
19
24
|
homepage: https://github.com/nucleom42/pub-sub
|
20
25
|
licenses:
|
21
26
|
- MIT
|