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
action()
View Source
action() :: {State.name(), trans()}
action() :: {State.name(), trans()}
bypasses() View Source
doc()
View Source
doc() :: String.t()
doc() :: String.t()
doc_key() View Source
docs() View Source
handler()
View Source
handler() :: module()
handler() :: module()
info() View Source
params()
View Source
params() :: term()
params() :: term()
result()
View Source
result() :: {handler(), [State.name()]}
result() :: {handler(), [State.name()]}
specs() View Source
trans()
View Source
trans() :: atom()
trans() :: atom()
transition()
View Source
transition() :: ({trans(), params()}, State.t() -> transition_ret())
transition() :: ({trans(), params()}, State.t() -> transition_ret())
transition_ret()
View Source
transition_ret() ::
{:next_state, State.name(), State.t()} | {:keep_state, State.t()}
transition_ret() :: {:next_state, State.name(), State.t()} | {:keep_state, State.t()}
Link to this section Functions
action(cs, action, params)
View Source
action(Ecto.Changeset.t(), trans(), params()) :: Ecto.Changeset.t()
action(Ecto.Changeset.t(), trans(), params()) :: Ecto.Changeset.t()
Executes action on a changeset with associated FSM
bypass(signature, body_block) View Source (macro)
Define a function of type bypass
, ie which can be applied on any state
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