Module: Omnes::Subscriber

Extended by:
Configurable
Defined in:
lib/omnes/subscriber.rb,
lib/omnes/subscriber/state.rb,
lib/omnes/subscriber/errors.rb,
lib/omnes/subscriber/adapter.rb,
lib/omnes/subscriber/adapter/method.rb,
lib/omnes/subscriber/adapter/sidekiq.rb,
lib/omnes/subscriber/adapter/active_job.rb,
lib/omnes/subscriber/adapter/method/errors.rb

Overview

Supscriptions provider for a Bus

This module allows an including class to use its context to create event handlers.

In its simplest form, you can match an event to a method in the class.

class MySubscriber
  include Omnes::Subscriber

  handle :foo, with: :my_handler

  def my_handler(event)
    # do_something
  end
end

Equivalent to the subscribe methods in Bus, you can also subscribe to all events or use a custom matcher:

class MySubscriber
  include Omnes::Subscriber

  handle_all                      with: :my_handler_one
  handle_with_matcher my_matcher, with: :my_handler_two

  def my_handler_one(event)
    # do_something
  end

  def my_handler_two(event)
    # do_something_else
  end
end

Another option is to let the event handlers be automatically discovered. You need to enable the autodiscover feature and prefix the event name with on_ for your handler name.

class MySubscriber
  include Omnes::Subscriber[
    autodiscover: true
  ]

  def on_foo(event)
    # do_something
  end
end

If you prefer, you can make autodiscover on by default:

Omnes.config.subscriber.autodiscover = true

You can specify your own autodiscover strategy. It must be something callable, transforming the event name into the handler name.

AUTODISCOVER_STRATEGY = ->(event_name) { event_name }

class MySubscriber
  include Omnes::Subscriber[
    autodiscover: true,
    autodiscover_strategy: AUTODISCOVER_STRATEGY
  ]

  def foo(event)
    # do_something
  end
end

You're not limited to using method names as event handlers. You can create your own adapters from the subscriber instance context.

ADAPTER = lambda do |instance, event|
  event.foo? ? instance.foo_true(event) : instance.foo_false(event)
end

class MySubscriber
  include Omnes::Subscriber

  handle :my_event, with: ADAPTER

  def foo_true(event)
    # do_something
  end

  def foo_false(event)
    # do_something_else
  end
end

Subscriber adapters can be leveraged to build integrations with background job libraries. See Adapter for what comes shipped with the library.

Once you've defined the event handlers, you can subscribe to a Bus instance:

MySubscriber.new.subscribe_to(bus)

Notice that a subscriber instance can only be subscribed once to the same bus. However, you can subscribe distinct instances to the same bus or the same instance to different buses.

Defined Under Namespace

Modules: Adapter, ClassMethods, InstanceMethods Classes: Module, MultipleSubscriberSubscriptionAttemptError, State

Constant Summary collapse

ON_PREFIX_STRATEGY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

->(event_name) { :"on_#{event_name}" }

Class Method Summary collapse

Class Method Details

.[](autodiscover: config.autodiscover, autodiscover_strategy: config.autodiscover_strategy) ⇒ Object

Includes with options

include Omnes::Subscriber[autodiscover: true]

Use regular include Omnes::Subscriber in case you want to use the defaults (which can be changed through configuration).

Parameters:

  • autodiscover (Boolean) (defaults to: config.autodiscover)
  • autodiscover_strategy (#call) (defaults to: config.autodiscover_strategy)


144
145
146
# File 'lib/omnes/subscriber.rb', line 144

def self.[](autodiscover: config.autodiscover, autodiscover_strategy: config.autodiscover_strategy)
  Module.new(autodiscover_strategy: autodiscover ? autodiscover_strategy : nil)
end

.configConfigurable::Config Originally defined in module Configurable

Returns the configuration class

Use this class to access readers and writers for the defined settings or nested configurations

.configure {|@config| ... } ⇒ Object Originally defined in module Configurable

Yields the configuration class

Yields:

See Also:

.included(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



149
150
151
# File 'lib/omnes/subscriber.rb', line 149

def self.included(klass)
  klass.include(self.[])
end

.nest_config(constant, name: default_nesting_name(constant)) ⇒ Object Originally defined in module Configurable

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

.setting(name, default:) ⇒ Object Originally defined in module Configurable

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.