Class: Trailblazer::Circuit::Wrap::Alterations

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/circuit/wrap.rb

Overview

Alterations#call finds alterations for ‘task` and applies them to `task_wrap`. Each alteration receives the result of the former one, starting with `task_wrap`.

This is used to add tracing steps, input/output contracts, and more at runtime, or to maintain specific static task_wraps, as for each step in an operation.

Note that an alteration doesn’t have to respect its ‘task_wrap` and can simply return an arbitrary Circuit.

DESIGN

By moving the “default” decision to this object, you can control what task gets wrapped with what wrap, allowing you to only wrap “focussed” tasks, for example.

It also allows to inject, say, a tracing alteration that is only applied to a specific task and returns all others unchanged.

Constant Summary collapse

PassThrough =
->(task_wrap) { task_wrap }

Instance Method Summary collapse

Constructor Details

#initialize(map: {}, default: [ PassThrough ]) ⇒ Alterations

Returns a new instance of Alterations.



90
91
92
# File 'lib/trailblazer/circuit/wrap.rb', line 90

def initialize(map: {}, default: [ PassThrough ])
  @default_alterations, @map = default, map
end

Instance Method Details

#call(task, target_wrap) ⇒ Object



95
96
97
98
# File 'lib/trailblazer/circuit/wrap.rb', line 95

def call(task, target_wrap)
  get(task). # list of alterations.
    inject(target_wrap) { |circuit, alteration| alteration.(circuit) }
end