module Trailblazer::Macro

Constants

Linear
NoopHandler

Public Class Methods

Model(model_class, action = nil, find_by_key = nil, id: 'model.build', not_found_terminus: false) click to toggle source
# File lib/trailblazer/macro/model.rb, line 5
def self.Model(model_class, action = nil, find_by_key = nil, id: 'model.build', not_found_terminus: false)
  task = Trailblazer::Activity::TaskBuilder::Binary(Model.new)

  injection = Trailblazer::Activity::TaskWrap::Inject::Defaults::Extension(
    :"model.class"          => model_class,
    :"model.action"         => action,
    :"model.find_by_key"    => find_by_key
  )

  options = { task: task, id: id, extensions: [injection] }
  options = options.merge(Linear::Output(:failure) => Linear::End(:not_found)) if not_found_terminus

  options
end
Nested(callable, id: "Nested( click to toggle source

{Nested} macro.

# File lib/trailblazer/macro/nested.rb, line 5
def self.Nested(callable, id: "Nested(#{callable})", auto_wire: [])
  if callable.is_a?(Class) && callable < Nested.operation_class
    warn %{[Trailblazer] Using the `Nested()` macro with operations and activities is deprecated. Replace `Nested(Create)` with `Subprocess(Create)`.}
    return Nested.operation_class.Subprocess(callable)
  end

  # dynamic
  task = Nested::Dynamic.new(callable, auto_wire: auto_wire)

  merge = [
    [Activity::TaskWrap::Pipeline.method(:insert_before), "task_wrap.call_task", ["Nested.compute_nested_activity", task.method(:compute_nested_activity)]],
    [Activity::TaskWrap::Pipeline.method(:insert_after),  "task_wrap.call_task", ["Nested.compute_return_signal", task.method(:compute_return_signal)]],
  ]

  task_wrap_extension = Activity::TaskWrap::Extension(merge: merge)

  {
    task:       task,
    id:         id,
    extensions: [task_wrap_extension],
    outputs:    task.outputs,
  }
end
Rescue(*exceptions, handler: NoopHandler, &block) click to toggle source
# File lib/trailblazer/macro/rescue.rb, line 5
def self.Rescue(*exceptions, handler: NoopHandler, &block)
  exceptions = [StandardError] unless exceptions.any?

  handler    = Rescue.deprecate_positional_handler_signature(handler)
  handler    = Trailblazer::Option(handler)

  # This block is evaluated by {Wrap}.
  rescue_block = ->((ctx, flow_options), **circuit_options, &nested_activity) do
    begin
      nested_activity.call
    rescue *exceptions => exception
      # DISCUSS: should we deprecate this signature and rather apply the Task API here?
      handler.call(exception, ctx, **circuit_options) # FIXME: when there's an error here, it shows the wrong exception!

      [Operation::Railway.fail!, [ctx, flow_options]]
    end
  end

  Wrap(rescue_block, id: "Rescue(#{SecureRandom.hex(4)})", &block)
  # FIXME: name
  # [ step, name: "Rescue:#{block.source_location.last}" ]
end
Wrap(user_wrap, id: "Wrap/ click to toggle source
# File lib/trailblazer/macro/wrap.rb, line 5
def self.Wrap(user_wrap, id: "Wrap/#{SecureRandom.hex(4)}", &block)
  activity = Class.new(Activity::FastTrack, &block) # This is currently coupled to {dsl-linear}.

  outputs  = activity.to_h[:outputs]
  outputs  = Hash[outputs.collect { |output| [output.semantic, output] }] # TODO: make that a helper somewhere.

  wrapped  = Wrap::Wrapped.new(activity, user_wrap, outputs)

  {task: wrapped, id: id, outputs: outputs}
end