Class: Crimson::NotificationBus

Inherits:
Object
  • Object
show all
Defined in:
lib/crimson/notification_bus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotificationBus

Returns a new instance of NotificationBus.



9
10
11
# File 'lib/crimson/notification_bus.rb', line 9

def initialize
  @notification_handlers = Hashie::Mash.new
end

Instance Attribute Details

#notification_handlersObject (readonly)

Returns the value of attribute notification_handlers.



7
8
9
# File 'lib/crimson/notification_bus.rb', line 7

def notification_handlers
  @notification_handlers
end

Instance Method Details

#notify(message) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/crimson/notification_bus.rb', line 27

def notify(message)
  unless notification_handlers.key?(message.id)
    raise ArgumentError, "[NotificationBus] Trying to notify unregistered '#{message.id}'."
  end

  notification_handlers[message.id].call(message)
end

#register(object) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/crimson/notification_bus.rb', line 13

def register(object)
  raise ArgumentError unless object.is_a?(Crimson::Object)
  raise ArgumentError if notification_handlers.key?(object.id)

  notification_handlers[object.id] = object.method(:on_event)
end

#unregister(object) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'lib/crimson/notification_bus.rb', line 20

def unregister(object)
  raise ArgumentError unless object.is_a?(Crimson::Object)
  raise ArgumentError unless notification_handlers.key?(object.id)

  notification_handlers.delete(object.id)
end