machinery v0.4.0 Machinery View Source
Main Machinery module.
It keeps the bunk of the Machinery logics, it’s the module that
will be imported with use
on the module that the state machine will
be implemented.
Parameters
opts
: A Keyword includingstates
andtransitions
.states
: A List of Atoms representing each state.transitions
: A List of Maps, including two keysfrom
andto
,to
might be an Atom or a List of Atoms.
Example
defmodule Project.User do
use Machinery,
states: [:created, :partial, :complete],
transitions: [
%{from: :created, to: [:partial, :complete]},
%{from: :partal, to: :complete}
]
end
Link to this section Summary
Functions
Main macro function that will be executed upon the load of the module using it
Triggers the transition of a struct to a new state if it passes the
existing guard clause, also runs any before or after callbacks.
It returns a tuple with {:ok, state}
, or {:error, "cause"}
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 transition
transition_to(struct(), atom()) :: {:ok, struct()} | {:error, String.t()}
Triggers the transition of a struct to a new state if it passes the
existing guard clause, also runs any before or after callbacks.
It returns a tuple with {:ok, state}
, or {:error, "cause"}
.
Parameters
struct
: A Struct based on a module using Machinery.next_state
: Atom of the next state you want to transition to.
Examples
Machinery.transition_to(%User{state: :partial}, :completed)
{:ok, %User{state: :completed}}