pub-sub 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb908098d16458af3705fe055544e14da2f80f4a43fbcb599404589cc1b91403
4
- data.tar.gz: 786dcf8e474e64f25ee9dc4eea3bf25987e7f196aaa6722a4b6f7bc7a25e3b6a
3
+ metadata.gz: 8ba7ce73cf0a207a83460614b4b53232f154f3b72a869c9511ad818cecfffa06
4
+ data.tar.gz: 13f2c58178f4820477279f27860167ec67f6b2dd0c426ad2649066990a824d8c
5
5
  SHA512:
6
- metadata.gz: de32890b8dfb6f2ea8519e9550d03df483d35cc9f3f0ad9b4dd03f3bfe66076abf83f6fbb64462574f9c4b101d5fdae349966fe9bc6358de0d161e65d045b649
7
- data.tar.gz: f8aaa20ca1421c2ee8482d4a8ed7a9af33252f718929b5cead527d88bf8bc48888d3d931ea766d301b4a4cf985e26d35efa04436b95b5b61b4c93e5cec78c0d9
6
+ metadata.gz: 7065628d70ee6e082e042369de6e220e950bbae0c3b1c209b42e8a4420371cea1787fd9fc4526e457dd6b3512abe56e1fe4a84914590c967fb8ae47181a20672
7
+ data.tar.gz: 539ccda4ef0683b3535781b5ca0826d891be979c087d1fea6156d779a7dd516f2c13a2db26c30515594629a213221fdc88be31623aaa12c2386f6fd53930212e
@@ -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
+
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PubSub
4
+ class Hash < Container
5
+ def initialize
6
+ @container = {}
7
+ end
8
+
9
+ def pub(event, args={})
10
+ iterable_subscriber_list = @container[event]
11
+ return false unless iterable_subscriber_list
12
+
13
+ clazzes = retrieve_klasses(iterable_subscriber_list)
14
+ fan_out(clazzes, args)
15
+ end
16
+
17
+ def sub(event, klazz_name)
18
+ mutex = Mutex.new
19
+
20
+ mutex.synchronize do
21
+ @container[event] = [] unless @container[event]
22
+ @container[event] << klazz_name unless @container[event].include?(klazz_name)
23
+ end
24
+ true
25
+ end
26
+
27
+ def unsub(event, klazz_name)
28
+ mutex = Mutex.new
29
+
30
+ mutex.synchronize do
31
+ return false unless @container[event]
32
+
33
+ @container[event].delete(klazz_name)
34
+ true
35
+ end
36
+ end
37
+ end
38
+ 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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov
@@ -17,6 +17,10 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
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
20
24
  homepage: https://github.com/nucleom42/pub-sub
21
25
  licenses:
22
26
  - MIT