machinery v0.15.0 Machinery View Source
This is the main Machinery module.
It keeps most of the Machinery logics, it’s the module that will be
imported with use
on the module responsible for the state machine.
Declare the states as an argument when importing Machinery
on the module
that will control your states transitions.
Machinery expects a Keyword
as argument with two keys states
and transitions
.
Parameters
opts
: A Keyword includingstates
andtransitions
.states
: A List of Strings representing each state.transitions
: A Map for each state and it allowed next state(s).
Example
defmodule YourProject.UserStateMachine do
use Machinery,
# The first state declared will be considered
# the intial state
states: ["created", "partial", "complete"],
transitions: %{
"created" => ["partial", "complete"],
"partial" => "completed"
}
end
Link to this section Summary
Functions
Main macro function that will be executed upon the load of the module using it
Start function that will trigger a supervisor for the Machinery.Transitions, a
GenServer that controls the state transitions and also starts another process,
if the interface
config is set to true
, for Machinery.Endpoint, a module
that uses Phoenix.Endpoint to expose new routes related to Machinery
Triggers the transition of a struct to a new state, accordinly to a specific
state machine module, if it passes any existing guard functions.
It also runs any before or after callbacks and returns a tuple with
{:ok, struct}
, or {:error, "reason"}
Link to this section Functions
Main macro function that will be executed upon the load of the module using it.
It basically stores the states and transitions.
It expects a Keyword
as argument with two keys states
and transitions
.
states
: A List of Strings representing each state.transitions
: A Map for each state and it allowed next state(s).
P.S. The first state declared will be considered the intial state
Start function that will trigger a supervisor for the Machinery.Transitions, a
GenServer that controls the state transitions and also starts another process,
if the interface
config is set to true
, for Machinery.Endpoint, a module
that uses Phoenix.Endpoint to expose new routes related to Machinery.
Triggers the transition of a struct to a new state, accordinly to a specific
state machine module, if it passes any existing guard functions.
It also runs any before or after callbacks and returns a tuple with
{:ok, struct}
, or {:error, "reason"}
.
Parameters
struct
: Thestruct
you want to transit to another state.state_machine_module
: The module that holds the state machine logic, where Machinery as imported.next_state
: String of the next state you want to transition to.
Examples
Machinery.transition_to(%User{state: :partial}, UserStateMachine, :completed)
{:ok, %User{state: :completed}}