Class: OmniService::Sequence

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Includes:
Strict
Defined in:
lib/omni_service/sequence.rb

Overview

Executes components sequentially, short-circuiting on first failure or shortcut. Context accumulates across components; each receives merged context from previous.

Examples:

Blog post creation pipeline

sequence(
  validate_params,           # Failure here stops the pipeline
  find_author,               # Adds :author to context
  create_post,               # Receives :author, adds :post
  notify_subscribers         # Receives :author and :post
)

With shortcut for idempotency

sequence(
  shortcut(find_existing_post),  # Success here exits early
  validate_params,
  create_post
)

Instance Method Summary collapse

Methods included from Strict

#call!

Constructor Details

#initialize(*args) ⇒ Sequence

Returns a new instance of Sequence.



29
30
31
# File 'lib/omni_service/sequence.rb', line 29

def initialize(*args, **)
  super(args.flatten(1), **)
end

Instance Method Details

#call(*params, **context) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/omni_service/sequence.rb', line 33

def call(*params, **context)
  component_wrappers.inject(OmniService::Result.build(self, params:, context:)) do |result, component|
    break result if result.failure? || result.shortcut?

    result.merge(component.call(*result.params, **result.context))
  end
end

#signatureObject



41
42
43
44
45
46
# File 'lib/omni_service/sequence.rb', line 41

def signature
  @signature ||= begin
    param_counts = component_wrappers.map { |component| component.signature.first }
    [param_counts.all? ? param_counts.max : nil, true]
  end
end