Class: SagaForge::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/saga_forge/router.rb

Overview

Boot-time event → saga-class registry + the shared row builder used by both publish paths. Registration happens in Base.inherited; reset on code reload (railtie to_prepare).

No internal locking around @classes: register/reset!/compile_all! are only ever called from the main autoloader (Base.inherited during load, railtie to_prepare during reload), and Rails' reloader interlock already serializes those against request threads.

Class Method Summary collapse

Class Method Details

.compile_all!Object

Forces every registered class to compile its Definition right now, so a broken saga (bad DSL, missing correlate_by, etc.) raises here — loudly, at boot/reload (§A.8) — instead of being silently skipped as a recipient the first time something happens to publish its event.



26
# File 'lib/saga_forge/router.rb', line 26

def compile_all! = saga_classes.each(&:definition)

.recipients_for(event_name) ⇒ Object



28
29
30
31
# File 'lib/saga_forge/router.rb', line 28

def recipients_for(event_name)
  event = event_name.to_sym
  @classes.select { |k| handler_for(k, event) }
end

.register(klass) ⇒ Object



14
15
16
# File 'lib/saga_forge/router.rb', line 14

def register(klass)
  @classes << klass unless @classes.include?(klass)
end

.reset!Object



18
# File 'lib/saga_forge/router.rb', line 18

def reset! = @classes = []

.resolve(event_name, payload) ⇒ Object

One fully-built Event attribute hash per recipient class. Raises MissingCorrelationError before anything is inserted — atomic publish.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/saga_forge/router.rb', line 35

def resolve(event_name, payload)
  payload = payload.with_indifferent_access
  recipients_for(event_name).map do |klass|
    {
      saga_class: klass.name,
      correlation_id: klass.definition.correlate(payload, event_name.to_sym),
      event_name: event_name.to_s,
      payload: payload,
      status: :pending
    }
  end
end

.saga_classesObject



20
# File 'lib/saga_forge/router.rb', line 20

def saga_classes = @classes