@@ -28,27 +28,28 @@ dependencies in `mix.exs`:
2828``` elixir
2929def deps do
3030 [
31- {:machinery , " ~> 0.4.0 " }
31+ {:machinery , " ~> 0.4.1 " }
3232 ]
3333end
3434```
3535
3636## Declaring States
3737
38- Declare the states you need pasing it as an argment when importing ` Machinery `
39- on the module that will control your states transitions.
38+ Declare the states as an argment when importing ` Machinery ` on the module that
39+ will control your states transitions.
4040
4141Machinery expects a ` Keyword ` as argument with two keys ` states ` and ` transitions ` .
4242
4343- ` states ` : A List of Atoms representing each state.
44- - ` transitions ` : A List of Maps, including two keys ` from ` and ` to ` , ` to ` might be an Atom or a List of Atoms .
44+ - ` transitions ` : A Map for each state and it allowed next state(s) .
4545
4646### Example
4747
4848``` elixir
4949defmodule YourProject .UserStateMachine do
5050 use Machinery ,
51- # The first state declared will be considered the intial state
51+ # The first state declared will be considered
52+ # the intial state
5253 states: [:created , :partial , :complete ],
5354 transitions: %{
5455 created: [:partial , :complete ],
5960
6061## Changing States
6162
62- To transit a struct into another state, you just need to call ` Machinery.transition_to/2 `
63+ To transit a struct into another state, you just need to call ` Machinery.transition_to/2 ` .
6364
6465### ` Machinery.transition_to/2 `
6566It takes two arguments:
6667
67- - ` struct ` : The struct you want to transit to another state
68- - ` next_event ` : An atom representing the next state you want the struct to transition to
68+ - ` struct ` : The ` struct ` you want to transit to another state.
69+ - ` next_event ` : An ` atom ` of the next state you want the struct to transition to.
70+
71+ ** Guard functions, before and after callbacks will be checked automatically.**
6972
7073``` elixir
7174Machinery .transition_to (your_struct, :next_state )
@@ -79,26 +82,27 @@ user = Accounts.get_user!(1)
7982UserStateMachine .transition_to (user, :partial )
8083```
8184
82- Guard functions, before and after callbacks will be checked automatically.
83-
8485## Guard functions
8586Create guard conditions by adding signatures of the ` guard_transition/2 `
8687function, pattern matching the desired state you want to guard.
8788
89+ ``` elixir
90+ def guard_transition (struct, :state ), do: true
91+ ```
92+
8893Guard conditions should return a boolean:
8994 - ` true ` : Guard clause will allow the transition.
9095 - ` false ` : Transition won't be allowed.
9196
97+ ### Example:
98+
9299``` elixir
93100defmodule YourProject .UserStateMachine do
94101 use Machinery ,
95- # The first state declared will be considered the intial state
96- states: [:created , :partial , :complete ],
97- transitions: %{
98- created: [:partial , :complete ],
99- partial: :completed
100- }
102+ states: [:created , :complete ],
103+ transitions: %{created: :complete }
101104
105+ # Guard the transition to the :complete state.
102106 def guard_transition (struct, :complete ) do
103107 Map .get (struct, :missing_fields ) == false
104108 end
@@ -108,27 +112,37 @@ end
108112## Before and After callbacks
109113
110114You can also use before and after callbacks to handle desired side effects and
111- reactions to a specific state transition, the implementation is pretty similar to
112- guard functions, you can just declare ` before_transition/2 ` and ``after_transition/2`.
113- Before and After callbacks should return the struct being manipulated.
115+ reactions to a specific state transition.
116+
117+ You can just declare ` before_transition/2 ` and ` after_transition/2 ` ,
118+ pattern matching the desired state you want to.
119+
120+ ** Make sure Before and After callbacks should return the struct.**
121+
122+ ``` elixir
123+ # callbacks should always return the struct.
124+ def before_transition (struct, :state ), do: struct
125+ def after_transition (struct, :state ), do: struct
126+ ```
127+
128+ ### Example:
114129
115130``` elixir
116131defmodule YourProject .UserStateMachine do
117132 use Machinery ,
118- # The first state declared will be considered the intial state
119133 states: [:created , :partial , :complete ],
120134 transitions: %{
121135 created: [:partial , :complete ],
122136 partial: :completed
123137 }
124138
125- def before_transition (struct, :partial ) do
126- # ... overall desired side effect
139+ def before_transition (struct, :partial ) do
140+ # ... overall desired side effects
127141 struct
128142 end
129143
130144 def after_transition (struct, :completed ) do
131- # ... overall desired side effect
145+ # ... overall desired side effects
132146 struct
133147 end
134148end
0 commit comments