module Shamu::Events::Support

Add event dispatching support to a {Services::Service}

Public Instance Methods

event_channel() click to toggle source

(see Support.event_channel)

# File lib/shamu/events/support.rb, line 25
def event_channel
  self.class.event_channel
end

Private Instance Methods

event!( message, channel: event_channel, **message_attrs ) click to toggle source

@!visibility public

Publish the given `message` to the {#events_service}.

@param [Events::Message, Symbol] message the custom event specific message to

publish.

@param [String] channel to publish to. @param [Hash] message_attrs arguments to use when creating an instance of `message`.

If `message` is a symbol, looks for a {Message} class in {ServiceNamespace}::{OptionalServiceDomain}Events::{name.caemlize}. @return [void]

# File lib/shamu/events/support.rb, line 44
def event!( message, channel: event_channel, **message_attrs )
  if message.is_a?( Symbol )
    message = self.class
                  .event_message_namespace
                  .const_get( message.to_s.camelize )
                  .new( message_attrs )
  end
  events_service.publish channel, message
end
event_message_namespace() click to toggle source

The module that holds the per-message event classes for the service. @return [Module]

# File lib/shamu/events/support.rb, line 77
def event_message_namespace
  @event_message_namespace ||=
    begin
      namespace = name.deconstantize
      return unless namespace.present?

      namespace = namespace.constantize
      domain    = name.demodulize.sub( /Service/, "" ).singularize

      # Must use exceptions instead of const_defined? so that rails has
      # a change to autoload the constant.
      begin
        namespace.const_get( "#{ domain }Events" )
      rescue NameError
        namespace.const_get( "Events" )
      end
    end
end