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

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

Instance Method Details

#event!(message, channel: event_channel)

This method returns an undefined value.

Publish the given message to the #events_service.

Parameters:

  • message (Events::Message)

    the custom event specific message to publish.

  • channel (String) (defaults to: event_channel)

    to publish to.



34
35
36
# File 'lib/shamu/events/support.rb', line 34

def event!( message, channel: event_channel )
  events_service.publish channel, message
end

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



48
49
50
51
52
53
54
55
56
# File 'lib/shamu/events/support.rb', line 48

def event_channel
  @event_channel ||= begin
    base_name = self.class.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