View Source Creating a pipeline
To create a pipeline, use Plumbery
in a module, and use pipeline
DSL keyword
to declare your pipeline.
defmodule Example do
use Plumbery
import Plumbery.Request
defp validate(req = %Plumbery.Request{command: 42}), do: assign(req, :validated, true)
defp validate(req), do: error(req, :no_luck)
defp act(req), do: success(req, "You are lucky")
pipeline :lucky_number do
pipe :validate
pipe :act
end
end
%{result: {:ok, "You are lucky"}, assigns: %{validated: true}} =
Example.lucky_number(%Plumbery.Request{command: 42})
%{result: {:error, :no_luck}} = Example.lucky_number(%Plumbery.Request{command: 41})
This example is very basic, but it demonstrates the main ideas of Plumbery. Let's see what the code does.
First, the Plumbery
module is used. This makes available the DSL. Next,
functions from Plumbery.Pipeline
are imported. This adds, among others,
error
and success
functions. These functions are used to set the result of
pipe execution. assign
function is used to set shared data that can be used in
subsequent pipes.
Next a couple of functions is defined. They will become pipes – the building blocks of pipeline.
Finally, a pipeline with two pipes is created. As you can see from the first
call example, the pipeline is compiled into a function that call the pipes in
order and passes the request, potentially modifying it. But the second call
example is actually more interesting – if any pipe returns an error, the
pipeline does not call any more pipes downstream and returns immediately. This
is the default behaviour, and it can be modified like many other aspects of
pipeline processing. Read the rest of the guides and the documentation for
Plumbery.Dsl
module to learn more.