Opus.Pipeline (Opus v0.7.0) View Source

Defines a pipeline.

A pipeline defines a single entry point function to start running the defined stages. A sample pipeline can be:

defmodule ArithmeticPipeline do
  use Opus.Pipeline

  step :to_integer, &:erlang.binary_to_integer/1
  step :double, with: & &1 * 2
end

The pipeline can be run calling a call/1 function which is defined by Opus.Pipeline. Pipelines are intended to have a single parameter and always return a tagged tuple {:ok, value} | {:error, error}. A stage returning {:error, error} halts the pipeline. The error value is an Opus.PipelineError struct which contains useful information to detect where the error was caused and why.

Exception Handling

All exceptions are converted to {:error, exception} tuples by default. You may let a stage raise an exception by providing the :raise option to a stage as follows:

defmodule ArithmeticPipeline do
  use Opus.Pipeline

  step :to_integer, &:erlang.binary_to_integer/1, raise: [ArgumentError]
end

Stage Filtering

You can select the stages of a pipeline to run using call/2 with the :except and :only options. Example:

# Runs only the stage with the :validate_params name
CreateUserPipeline.call(params, only: [:validate_params]
# Runs all the stages except the selected ones
CreateUserPipeline.call(params, except: :send_notification)

Link to this section Summary

Link to this section Types

Specs

opts() :: [only: [atom()], except: [atom()]]

Specs

result() :: {:ok, any()} | {:error, Opus.PipelineError.t()}

Link to this section Functions

Link to this macro

check(name, opts \\ [])

View Source (macro)
Link to this macro

link(name, opts \\ [])

View Source (macro)
Link to this macro

skip(name, opts \\ [])

View Source (macro)
Link to this macro

step(name, opts \\ [])

View Source (macro)
Link to this macro

tee(name, opts \\ [])

View Source (macro)