servus 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd735552263fe1b0d29ee266359703e365ce3e24cc5499edb73dab9092e9bc65
4
- data.tar.gz: 98f05a4f76a8b5804ec3f4178f6f4ae488e92e293d8bbe49702709b4e67981db
3
+ metadata.gz: 1ed0c555ab8879d5f5e32ecfed8109214468bbd3f9a397a4d6b1a79df51cc2e8
4
+ data.tar.gz: e518983727d4fad53fe08ef7863692cfa06c002b2a4a9248b38b8a25c3f2371d
5
5
  SHA512:
6
- metadata.gz: b202484d40d9571bf9941b28cbb1fedd06c06fe508d26728e5698e13c8f3cd9705035e4e065d78965235f27f0a428073a410220d46bb55a4c5b2e70c0057e093
7
- data.tar.gz: 7bd5c3bd0f6b54a137511941b1141e8b299ae366bcce6509a0f812261004e710de9a17b240e87a901a0cd1b38f1312fe6747be201e572780795289a4afabbdbc
6
+ metadata.gz: ddece449340240ced0d0df59d9c1eb9ed79e5e03ffd8ffd6cb5a402dd8d206d13489ef9da9d7e7b49dc98d2b2abff5cb78f235ccfdda6d085abfa96f36b4fb14
7
+ data.tar.gz: ffc7bd55d38b027005eb89187b4987776e546a9bb85d33c2566848704d1f787fb2e985700b921a57add2356ce67e9f877985fdae7d86fc52767055c2ecdb05b9
data/lib/servus/config.rb CHANGED
@@ -90,6 +90,36 @@ module Servus
90
90
  # end
91
91
  attr_accessor :logger
92
92
 
93
+ # What to add to every event payload, beside what each event is about.
94
+ #
95
+ # A callable given the payload as built, the event's name and the service
96
+ # emitting it (+nil+ for {Servus::Event.emit}), returning the payload to
97
+ # validate and dispatch. Applied on both emission paths, before the payload
98
+ # is validated, so an Event's schema can require what it adds. Use it for
99
+ # what every event should say the same way, such as who caused it, rather
100
+ # than having each service build it into its own payloads.
101
+ #
102
+ # @return [#call, nil] the enricher, or nil to emit payloads as built
103
+ #
104
+ # @example Say who caused every event
105
+ # config.event_payload_enricher = lambda do |payload, event_name:, service:|
106
+ # payload.merge(meta: { actor: Current.user&.id })
107
+ # end
108
+ attr_accessor :event_payload_enricher
109
+
110
+ # The payload an event is emitted with: as built, or as the configured
111
+ # {#event_payload_enricher} returns it.
112
+ #
113
+ # @param payload [Hash] the payload as built
114
+ # @param event_name [Symbol] the event being emitted
115
+ # @param service [Servus::Base, nil] the service emitting it, if one is
116
+ # @return [Hash] the payload to validate and dispatch
117
+ def enrich_event_payload(payload, event_name:, service: nil)
118
+ return payload unless event_payload_enricher
119
+
120
+ event_payload_enricher.call(payload, event_name:, service:)
121
+ end
122
+
93
123
  # The ordered list of routers that resolve invocations for events.
94
124
  #
95
125
  # The Bus iterates routers in order, collects invocations, deduplicates
data/lib/servus/event.rb CHANGED
@@ -202,6 +202,9 @@ module Servus
202
202
  #
203
203
  # Provides a type-safe, discoverable way to emit events from anywhere in
204
204
  # the application (controllers, jobs, rake tasks) without creating a service.
205
+ # The payload is given to the configured
206
+ # {Servus::Config#event_payload_enricher}, with no service, before it is
207
+ # validated.
205
208
  #
206
209
  # @param payload [Hash] the event payload
207
210
  # @return [void]
@@ -226,6 +229,7 @@ module Servus
226
229
  def emit(payload)
227
230
  raise 'No event configured. Call event_name :name first.' unless @event_name
228
231
 
232
+ payload = Servus.config.enrich_event_payload(payload, event_name: @event_name)
229
233
  Servus::Support::Validator.validate_event_payload!(self, payload)
230
234
 
231
235
  Servus::Events::Bus.emit(@event_name, payload)
@@ -146,7 +146,9 @@ module Servus
146
146
  end
147
147
  end
148
148
 
149
- # Emits events for a specific trigger with the given result.
149
+ # Emits events for a specific trigger with the given result. Each payload
150
+ # is built, then given to the configured
151
+ # {Servus::Config#event_payload_enricher}, then validated and dispatched.
150
152
  #
151
153
  # @param trigger [Symbol] the trigger type (:success, :failure, :error!)
152
154
  # @param result [Servus::Support::Response] the service result
@@ -156,7 +158,9 @@ module Servus
156
158
  self.class.emissions_for(trigger).each do |emission|
157
159
  next unless emission_condition_met?(emission, result)
158
160
 
159
- payload = build_event_payload(emission, result)
161
+ payload = Servus.config.enrich_event_payload(build_event_payload(emission, result),
162
+ event_name: emission[:event_name],
163
+ service: self)
160
164
  validate_event_payload!(emission[:event_name], payload)
161
165
  Servus::Events::Bus.emit(emission[:event_name], payload)
162
166
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Servus
4
- VERSION = '1.3.0'
4
+ VERSION = '1.3.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Scholl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-01 00:00:00.000000000 Z
11
+ date: 2026-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport