View Source Pluggable.Token protocol (pluggable v1.0.1)
Token protocol to be used with the Pluggable
and Pluggable.Builder
modules.
When implementing pipelines using this library, a token holds the state and is passed on from step to step within the pipeline.
deriving-pluggable-token
Deriving Pluggable.Token
The simplest way to use this library is to define a token module which derives
Pluggable.Token
and defines a struct which, among others defines the keys:
:halted
- the boolean status on whether the pipeline was halted:assigns
- shared user data as a map
Example:
defmodule MyPipeline.Token do
@derive Pluggable.Token
defstruct [
halted: false,
assigns: %{},
# other state
]
end
If the fields holding these two states are named differently, pass the fields
as options to @derive
:
defmodule MyPipeline.Token do
@derive {Pluggable.Token, halted_key: :stopped, assigns_key: :shared_state}
defstruct [
stopped: false,
shared_state: %{},
# other state
]
end
implementing-pluggable-token
Implementing Pluggable.Token
Pluggable.Token
can be implemented. The following is the default implementation
when deriving Pluggable.Token
defmodule MyPipeline.Token do
defstruct [
halted: nil,
assigns: %{},
# other state
]
end
defimpl Pluggable.Token, for: MyPipeline.Token do
def halted?(token), do: token.halted
def halt(token), do: %{token | halted: true}
def assign(%MyPipeline.Token{assigns: assigns} = token, key, value) when is_atom(key) do
%{token | assigns: Map.put(assigns, key, value)}
end
end
Link to this section Summary
Functions
Assigns a value to a key in the shared user data map.
Halts the Pluggable pipeline by preventing further steps downstream from being
invoked. See the docs for Pluggable.Builder
for more information on halting a
Pluggable pipeline.
Returns the boolean status on whether the pipeline was halted
Link to this section Types
@type t() :: term()
Link to this section Functions
Assigns a value to a key in the shared user data map.
Halts the Pluggable pipeline by preventing further steps downstream from being
invoked. See the docs for Pluggable.Builder
for more information on halting a
Pluggable pipeline.
Returns the boolean status on whether the pipeline was halted