Module: Shamu::Events::Support

Extended by:
ActiveSupport::Concern
Defined in:
lib/shamu/events/support.rb

Overview

Add event dispatching support to a Services::Service

Dependencies collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#events_serviceEvents::EventsService

Returns the events service to publish messages to.

Returns:



17
# File 'lib/shamu/events/support.rb', line 17

attr_dependency :events_service, Events::EventsService

Class Method Details

.event_channelString

The channel to publish events to. Defaults to the transformed name of the service class.

Users::UsersService              => users
Users::ProfileService            => users/profile
Users::Profiles::ProfilesService => users/profiles

Returns:

  • (String)

    the name of the channel.



65
66
67
68
69
70
71
72
73
# File 'lib/shamu/events/support.rb', line 65

def event_channel
  @event_channel ||= begin
    base_name = name || "Events"
    parts     = base_name.split( "::" )
    parts[-1].sub!( /Service$/, "" )
    parts.pop if parts[-1] == parts[-2] || ( parts.length > 1 && parts[-1].blank? )
    parts.join( "/" ).underscore
  end
end

.event_message_namespaceModule

The module that holds the per-message event classes for the service.

Returns:

  • (Module)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

Instance Method Details

#event!(message, channel: event_channel, **message_attrs)

This method returns an undefined value.

Publish the given message to the #events_service.

instance of message.

If message is a symbol, looks for a Message class in ServiceNamespace::OptionalServiceDomainEvents::Shamu::Events::Support.namename.caemlize.

Parameters:

  • message (Events::Message, Symbol)

    the custom event specific message to publish.

  • channel (String) (defaults to: event_channel)

    to publish to.

  • message_attrs (Hash)

    arguments to use when creating an



44
45
46
47
48
49
50
51
52
# 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_channelObject



25
26
27
# File 'lib/shamu/events/support.rb', line 25

def event_channel
  self.class.event_channel
end