ecto_fsm v0.2.0 Ecto.FSM View Source

Handle Ecto structures status through FSM

Provides macros for defining FSM

Defines FSM with transition/2 and bypass/2 macros.

Caller module is added the following functions:

  • fsm() :: Ecto.FSM.specs()
  • docs() :: Ecto.FSM.docs()

Transition functions must return Ecto.FSM.transition_ret.

Examples:

iex> defmodule Elixir.Door do
...>   use Ecto.FSM
...> 
...>   @doc "Close to open"
...>   @to [:opened]
...>   transition closed({:open, _}, s) do
...>     {:next_state, :opened, s}
...>   end
...> 
...>   @doc "Close to close"
...>   transition closed({:close, _}, s) do
...>     {:next_state, :closed, s}
...>   end
...> 
...>   transition closed({:else, _}, s) do
...>     {:next_state, :closed, s}
...>   end
...> 
...>   @doc "Open to open"
...>   transition opened({:open, _}, s) do
...>     {:next_state, :opened, s}
...>   end
...> 
...>   @doc "Open to close"
...>   @to [:closed]
...>   transition opened({:close, _}, s) do
...>     {:next_state, :closed, s}
...>   end
...> 
...>   transition opened({:else, _}, s) do
...>     {:next_state, :opened, s}
...>   end
...>
...>   @doc "Force the door"
...>   bypass force(_, s) do
...>     {:next_state, :destroyed, s}
...>   end
...> end
...> Door.fsm()
%{
  {:closed, :close} => {Door, [:closed]}, {:closed, :else} => {Door, [:closed]},
  {:closed, :open} => {Door, [:opened]}, {:opened, :close} => {Door, [:closed]},
  {:opened, :else} => {Door, [:opened]}, {:opened, :open} => {Door, [:opened]}
}
...> Door.docs()
%{
  {:transition_doc, :closed, :close} => "Close to close",
  {:transition_doc, :closed, :else} => nil,
  {:transition_doc, :closed, :open} => "Close to open",
  {:transition_doc, :opened, :close} => "Open to close",
  {:transition_doc, :opened, :else} => nil,
  {:transition_doc, :opened, :open} => "Open to open",
  {:event_doc, :force} => "Force the door"
}

Link to this section Summary

Functions

Executes action on a changeset with associated FSM

Define a function of type bypass, ie which can be applied on any state

Define a function of type transition describing a state and its transition. The function name is the state name, the transition is the first argument. A state object can be modified and is the second argument

Link to this section Types

Link to this type

action() View Source
action() :: {State.name(), trans()}

Link to this type

bypasses() View Source
bypasses() :: %{optional(trans()) => handler()}

Link to this type

doc_key() View Source
doc_key() :: {:transition_doc, State.name(), trans()} | {:event_doc, trans()}

Link to this type

docs() View Source
docs() :: %{optional(doc_key()) => doc()}

Link to this type

info() View Source
info() :: {:transition, doc()} | {:bypass, doc()}

Link to this type

result() View Source
result() :: {handler(), [State.name()]}

Link to this type

specs() View Source
specs() :: %{optional(action()) => result()}

Link to this type

transition() View Source
transition() :: ({trans(), params()}, State.t() -> transition_ret())

Link to this type

transition_ret() View Source
transition_ret() ::
  {:next_state, State.name(), State.t()} | {:keep_state, State.t()}

Link to this section Functions

Executes action on a changeset with associated FSM

Link to this macro

bypass(signature, body_block) View Source (macro)

Define a function of type bypass, ie which can be applied on any state

Link to this macro

transition(signature, body_block) View Source (macro)

Define a function of type transition describing a state and its transition. The function name is the state name, the transition is the first argument. A state object can be modified and is the second argument.

deftrans opened({:close_door, _params}, state) do
  {:next_state, :closed, state}
end