Module: Omnes::Event

Extended by:
Configurable
Defined in:
lib/omnes/event.rb

Overview

Event mixin for custom classes

Any instance of a class including this one can be used as a published event (see Bus#publish).

class MyEvent
  include Omnes::Event

  attr_reader :event

  def initialize(id:)
    @id = id
  end
end

bus = Omnes::Bus.new
bus.register(:my_event)
bus.subscribe(:my_event) do |event|
  puts event.id
end
bus.publish(MyEvent.new(1))

Constant Summary collapse

DEFAULT_NAME_BUILDER =

Generates the event name for an event instance

It returns the underscored class name, with an Event suffix removed if present. E.g:

  • Foo -> :foo
  • FooEvent -> :foo
  • FooBar -> :foo_bar
  • FBar -> :f_bar
  • Foo::Bar -> :foo_bar

You can also use your custom name builder. It needs to be something callable taking the instance as argument and returning a Symbol:

my_name_builder = ->(instance) { instance.class.name.to_sym }
Omnes.config.event.name_builder = my_name_builder

Returns:

  • (Symbol)
lambda do |instance|
  instance.class.name
          .chomp("Event")
          .gsub(/([[:alpha:]])([[:upper:]])/, '\1_\2')
          .gsub("::", "_")
          .downcase
          .to_sym
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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:

.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.

Instance Method Details

#omnes_event_nameSymbol

Event name

Returns:

  • (Symbol)

See Also:



68
69
70
# File 'lib/omnes/event.rb', line 68

def omnes_event_name
  Omnes::Event.config.name_builder.(self)
end