Module: PagesCore::PubSub

Defined in:
lib/pages_core/pub_sub.rb

Defined Under Namespace

Classes: Subscriber

Class Method Summary collapse

Class Method Details

.publish(name, payload = {}) ⇒ Object


4
5
6
7
8
# File 'lib/pages_core/pub_sub.rb', line 4

def publish(name, payload = {})
  payload_struct = OpenStruct.new(payload)
  subscribers.select { |s| s.name == name }
             .each { |s| s.call(payload_struct) }
end

.subscribe(name, &block) ⇒ Object


10
11
12
13
14
# File 'lib/pages_core/pub_sub.rb', line 10

def subscribe(name, &block)
  subscriber = PagesCore::PubSub::Subscriber.new(name, block)
  subscribers << subscriber
  subscriber
end

.subscribersObject


16
17
18
# File 'lib/pages_core/pub_sub.rb', line 16

def subscribers
  @subscribers ||= []
end

.unsubscribe(subscriber) ⇒ Object


20
21
22
# File 'lib/pages_core/pub_sub.rb', line 20

def unsubscribe(subscriber)
  @subscribers = subscribers.reject { |s| s == subscriber }
end