machinery v0.4.1 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. The first state declared will be considered the intial state

Parameters

  • opts: A Keyword including states and transitions.

    • states: A List of Atoms representing each state.
    • transitions: A Map for each state and it allowed next state(s).

Example

  defmodule Project.User do
    use Machinery,
      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

Triggers the transition of a struct to a new state, if it passes any existing guard clause, also runs any before or after callbacks. It returns a tuple with {:ok, struct}, or {:error, "reason"}

Link to this section Functions

Link to this macro __using__(opts) View Source (macro)

Main macro function that will be executed upon the load of the module using it.

It basically stores the states and transition

Link to this function transition_to(struct, next_state) View Source
transition_to(struct(), atom()) ::
  {:ok, struct()} |
  {:error, String.t()}

Triggers the transition of a struct to a new state, if it passes any existing guard clause, also runs any before or after callbacks. It returns a tuple with {:ok, struct}, or {:error, "reason"}.

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}}