Skip to content

Commit 47d0742

Browse files
committed
Change option to
A should behave as the elixir guard check, which returns a boolean. In the case of , it was returning to the next state so that a should be a better word for its purpose
1 parent 0967e21 commit 47d0742

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You can install `machinist` by adding it to your list of dependencies in `mix.e
1717
```elixir
1818
def deps do
1919
[
20-
{:machinist, "~> 1.0.0"}
20+
{:machinist, "~> 2.0.0"}
2121
]
2222
end
2323
```
@@ -84,20 +84,20 @@ iex> Door.transit(door_opened, event: "lock")
8484
{:error, :not_allowed}
8585
```
8686

87-
## Guard conditions
87+
## Conditions
8888

8989
We could also implement a state machine for an electronic door which should validate a passcode to unlock it. In this scenario, the `machinist` allows us to provide a function to evaluate a condition and return the new state.
9090

9191
Check out the diagram below representing it:
9292

9393
![state-machine-diagram](./assets/check-passcode.png)
9494

95-
And to have this condition for the `unlock` event, use the `event` macro passing the `guard` option with a one-arity function:
95+
And to have this condition for the `unlock` event, use the `event` macro passing the `cond` option with a one-arity function:
9696

9797
```elixir
9898
# ..
9999
transitions do
100-
event "unlock", guard: &check_passcode/1 do
100+
event "unlock", cond: &check_passcode/1 do
101101
from :locked, to: :unlocked
102102
from :locked, to: :locked
103103
end
@@ -108,7 +108,7 @@ defp check_passcode(door) do
108108
end
109109
```
110110

111-
So when we call `Door.transit(%Door{state: :locked}, event: "unlock")` the guard function `check_passcode/1` will be called with the struct door as the first parameter and returns the new state to be set.
111+
So when we call `Door.transit(%Door{state: :locked}, event: "unlock")` the function `check_passcode/1` will be called with the struct door as the first parameter and returns the new state to be set.
112112

113113
### Setting a different attribute name that holds the state
114114

@@ -170,7 +170,7 @@ defmodule SelectionProcess.V1 do
170170
from :new, to: :registered, event: "register"
171171
from :registered, to: :started_test, event: "start_test"
172172

173-
event "send_test", guard: &check_score/1 do
173+
event "send_test", cond: &check_score/1 do
174174
from :started_test, to: :approved
175175
from :started_test, to: :reproved
176176
end

lib/machinist.ex

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ defmodule Machinist do
118118

119119
@doc """
120120
Defines an `event` block grouping a same-event `from` -> `to` transitions
121-
with a guard function that should evaluates a condition and returns a new state.
121+
with a cond option that should receive a function that returns a new state.
122122
123-
event "update_score"", guard: &check_score/1 do
123+
event "update_score"", cond: &check_score/1 do
124124
from :test, to: :approved
125125
from :test, to: :reproved
126126
end
@@ -129,7 +129,7 @@ defmodule Machinist do
129129
if score >= 70, do: :approved, else: :reproved
130130
end
131131
"""
132-
defmacro event(event, [guard: func], do: {:__block__, line, content}) do
132+
defmacro event(event, [cond: func], do: {:__block__, line, content}) do
133133
content = prepare_transitions(event, func, content)
134134

135135
quote bind_quoted: [content: content, line: line] do
@@ -195,16 +195,16 @@ defmodule Machinist do
195195
[prepare_transition(event, head) | prepare_transitions(event, tail)]
196196
end
197197

198-
defp prepare_transitions(event, guard_func, [head | _]) do
199-
prepare_transition(event, guard_func, head)
198+
defp prepare_transitions(event, cond_func, [head | _]) do
199+
prepare_transition(event, cond_func, head)
200200
end
201201

202202
defp prepare_transition(event, {:from, _line, [from, to]}) do
203203
define_transition(from, to ++ [event: event])
204204
end
205205

206-
defp prepare_transition(event, guard_func, {:from, _line, [from, _to]}) do
207-
define_transition(from, to: guard_func, event: event)
206+
defp prepare_transition(event, cond_func, {:from, _line, [from, _to]}) do
207+
define_transition(from, to: cond_func, event: event)
208208
end
209209

210210
defp define_transitions(_state, []), do: []
@@ -283,7 +283,7 @@ defmodule Machinist.NoLongerSupportedSyntaxError do
283283
@impl true
284284
def exception(state: state, to: new_state_func, event: event) do
285285
new_dsl = ~s"""
286-
event "#{event}", guard: #{Macro.to_string(new_state_func)} do
286+
event "#{event}", cond: #{Macro.to_string(new_state_func)} do
287287
from :#{state}, to: :your_new_state
288288
end
289289
"""
@@ -292,7 +292,7 @@ defmodule Machinist.NoLongerSupportedSyntaxError do
292292
293293
#{IO.ANSI.reset()}`from` macro does not accept a function as a value to `:to` anymore
294294
295-
Instead use the `event` macro passing the function as a guard option:
295+
Instead, use the `event` macro passing the function as a `:cond` option:
296296
297297
#{new_dsl}
298298
"""

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Machinist.MixProject do
22
use Mix.Project
33

4-
@version "1.0.0"
4+
@version "2.0.0"
55
@repo_url "https://github.com/norbajunior/machinist"
66

77
def project do

test/machinist_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ defmodule MachinistTest do
195195
from :form2, to: :tests_in_progress
196196
end
197197

198-
event "update_test_score", guard: &check_status/1 do
198+
event "update_test_score", cond: &check_status/1 do
199199
from :tests_in_progress, to: :tests_in_progress
200200
from :tests_in_progress, to: :tests_reproved
201201
from :tests_in_progress, to: :tests_approved
@@ -273,7 +273,7 @@ defmodule MachinistTest do
273273
use Machinist
274274

275275
transitions do
276-
event "start_interview", guard: &which_interview/1 do
276+
event "start_interview", cond: &which_interview/1 do
277277
from :tests_approved do
278278
to :interview_1
279279
to :interview_2
@@ -294,9 +294,9 @@ defmodule MachinistTest do
294294
295295
\e[0m`from` macro does not accept a function as a value to `:to` anymore
296296
297-
Instead use the `event` macro passing the function as a guard option:
297+
Instead, use the `event` macro passing the function as a `:cond` option:
298298
299-
event "start_interview", guard: &which_interview/1 do
299+
event "start_interview", cond: &which_interview/1 do
300300
from :score_updated, to: :your_new_state
301301
end
302302
@@ -326,9 +326,9 @@ defmodule MachinistTest do
326326
327327
\e[0m`from` macro does not accept a function as a value to `:to` anymore
328328
329-
Instead use the `event` macro passing the function as a guard option:
329+
Instead, use the `event` macro passing the function as a `:cond` option:
330330
331-
event "start_interview", guard: &which_interview/1 do
331+
event "start_interview", cond: &which_interview/1 do
332332
from :score_updated, to: :your_new_state
333333
end
334334

0 commit comments

Comments
 (0)