Class: Octiron::Events::Bus

Inherits:
Object
  • Object
show all
Includes:
Support::Identifiers
Defined in:
lib/octiron/events/bus.rb

Overview

Implements and in-process pub-sub events broadcaster allowing multiple observers to subscribe to different events.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Identifiers

#identify

Methods included from Support::Constantize

#constantize

Methods included from Support::CamelCase

#camel_case

Constructor Details

#initialize(default_namespace = ::Octiron::Events) ⇒ Bus

Returns a new instance of Bus.

Parameters:

  • default_namespace (Symbol) (defaults to: ::Octiron::Events)

    The default namespace to look in for Event classes.



27
28
29
30
# File 'lib/octiron/events/bus.rb', line 27

def initialize(default_namespace = ::Octiron::Events)
  @default_namespace = default_namespace.to_s
  clear
end

Instance Attribute Details

#default_namespaceString (readonly)

Returns the default namespace to search for events.

Returns:

  • (String)

    the default namespace to search for events



22
23
24
# File 'lib/octiron/events/bus.rb', line 22

def default_namespace
  @default_namespace
end

Instance Method Details

#clearObject

Clears all event handlers



34
35
36
# File 'lib/octiron/events/bus.rb', line 34

def clear
  @handlers = {}
end

#publish(event) ⇒ Object Also known as: broadcast, notify

Broadcast an event. This is an instance of a class provided to #subscribe previously.

Parameters:

  • event (Object)

    the event to publish



77
78
79
80
81
82
83
84
85
# File 'lib/octiron/events/bus.rb', line 77

def publish(event)
  event_name = event
  if not event.is_a?(Hash)
    event_name = event.class.to_s
  end
  handlers_for(event_name, false).each do |handler|
    handler.call(event)
  end
end

#subscribe(event_id, handler_object = nil, &handler_proc) ⇒ Object Also known as: register

Subscribe an event handler to an event.

Parameters:

  • event_id (Class, String, other)

    A class or String naming an event class.

  • handler_object (Object) (defaults to: nil)

    Handler object that must implement a ‘#call` method accepting an instance of the event class provided in the first parameter. If nil, a block needs to be provided.

  • handler_proc (Proc)

    Handler block that accepts an instance of the event class provided in the first parameter. If nil, a handler object must be provided.

Returns:

  • The class represented by the event_id, as a String name.



49
50
51
52
53
# File 'lib/octiron/events/bus.rb', line 49

def subscribe(event_id, handler_object = nil, &handler_proc)
  return with_handlers(event_id, handler_object, handler_proc) do |hlist, h|
    hlist << h
  end
end

#unsubscribe(event_id, handler_object = nil, &handler_proc) ⇒ Object

Unsubscribe an event handler from an event.

Parameters:

  • event_id (Class, String, other)

    A class or String naming an event class.

  • handler_object (Object) (defaults to: nil)

    Handler object that must implement a ‘#call` method accepting an instance of the event class provided in the first parameter. If nil, a block needs to be provided.

  • handler_proc (Proc)

    Handler block that accepts an instance of the event class provided in the first parameter. If nil, a handler object must be provided.

Returns:

  • The class represented by the event_id, as a String name.



67
68
69
70
71
# File 'lib/octiron/events/bus.rb', line 67

def unsubscribe(event_id, handler_object = nil, &handler_proc)
  return with_handlers(event_id, handler_object, handler_proc) do |hlist, h|
    hlist.delete(h)
  end
end