Class: PubSub::Hash

Inherits:
Container show all
Defined in:
lib/pub_sub/hash.rb

Instance Method Summary collapse

Constructor Details

#initializeHash

Returns a new instance of Hash.



6
7
8
# File 'lib/pub_sub/hash.rb', line 6

def initialize
  RequestStore.store[:hash] = {}
end

Instance Method Details

#pub(event, args = {}) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/pub_sub/hash.rb', line 10

def pub(event, args={})
  iterable_subscriber_list = RequestStore.store[:hash][event]
  return false unless iterable_subscriber_list

  clazzes = retrieve_klasses(iterable_subscriber_list)
  fan_out(clazzes, args)
end

#sub(event, klazz_name) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/pub_sub/hash.rb', line 18

def sub(event, klazz_name)
  mutex = Mutex.new

  mutex.synchronize do
    RequestStore.store[:hash][event] = [] unless RequestStore.store[:hash][event]
    RequestStore.store[:hash][event] << klazz_name unless RequestStore.store[:hash][event].include?(klazz_name)
  end
  true
end

#unsub(event, klazz_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/pub_sub/hash.rb', line 28

def unsub(event, klazz_name)
  mutex = Mutex.new

  mutex.synchronize do
    return false unless RequestStore.store[:hash][event]

    RequestStore.store[:hash][event].delete(klazz_name)
    true
  end
end