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
-
#events_service ⇒ Events::EventsService
The events service to publish messages to.
Class Method Summary collapse
-
.event_channel ⇒ String
The channel to publish events to.
-
.event_message_namespace ⇒ Module
The module that holds the per-message event classes for the service.
Instance Method Summary collapse
-
#event!(message, channel: event_channel, **message_attrs)
Publish the given
message
to the #events_service. - #event_channel ⇒ Object
Instance Attribute Details
#events_service ⇒ Events::EventsService
Returns the events service to publish messages to.
17 |
# File 'lib/shamu/events/support.rb', line 17 attr_dependency :events_service, Events::EventsService |
Class Method Details
.event_channel ⇒ String
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
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_namespace ⇒ Module
The module that holds the per-message event classes for the service.
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 ||= 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.
44 45 46 47 48 49 50 51 52 |
# File 'lib/shamu/events/support.rb', line 44 def event!( , channel: event_channel, ** ) if .is_a?( Symbol ) = self.class . .const_get( .to_s.camelize ) .new( ) end events_service.publish channel, end |
#event_channel ⇒ Object
25 26 27 |
# File 'lib/shamu/events/support.rb', line 25 def event_channel self.class.event_channel end |