Class: Easyop::Events::Bus::Custom

Inherits:
Base
  • Object
show all
Defined in:
lib/easyop/events/bus/custom.rb

Overview

Wraps any user-supplied bus adapter.

The adapter must respond to:

#publish(event)
#subscribe(pattern, &block)

Optionally:

#unsubscribe(handle)

Examples:

Wrapping a RabbitMQ adapter

class MyRabbitBus
  def publish(event) = rabbit.publish(event.to_h, routing_key: event.name)
  def subscribe(pattern, &block) = rabbit.subscribe(pattern) { |msg| block.call(reconstruct(msg)) }
end

Easyop::Events::Registry.bus = MyRabbitBus.new

Passing via Custom wrapper explicitly

Easyop::Events::Registry.bus = Easyop::Events::Bus::Custom.new(MyRabbitBus.new)

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ Custom

Returns a new instance of Custom.

Parameters:

  • adapter (Object)

    must respond to #publish and #subscribe

Raises:

  • (ArgumentError)

    if adapter does not meet the interface



28
29
30
31
32
33
34
35
# File 'lib/easyop/events/bus/custom.rb', line 28

def initialize(adapter)
  unless adapter.respond_to?(:publish) && adapter.respond_to?(:subscribe)
    raise ArgumentError,
      "Custom bus adapter must respond to #publish(event) and " \
      "#subscribe(pattern, &block). Got: #{adapter.inspect}"
  end
  @adapter = adapter
end

Instance Method Details

#publish(event) ⇒ Object



37
38
39
# File 'lib/easyop/events/bus/custom.rb', line 37

def publish(event)
  @adapter.publish(event)
end

#subscribe(pattern, &block) ⇒ Object



41
42
43
# File 'lib/easyop/events/bus/custom.rb', line 41

def subscribe(pattern, &block)
  @adapter.subscribe(pattern, &block)
end

#unsubscribe(handle) ⇒ Object



45
46
47
# File 'lib/easyop/events/bus/custom.rb', line 45

def unsubscribe(handle)
  @adapter.unsubscribe(handle) if @adapter.respond_to?(:unsubscribe)
end