Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
norbajunior committed Jul 20, 2022
1 parent 2c65488 commit 9a33567
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ iex> Door.transit(door_opened, event: "lock")

## Guard conditions

We also could be implementing a state machine for an eletronic door which should validate the passcode to unlock it. In this scenario we should have a condition to change the state based on the passcode is valid or not. Check out the diagram below representing it:
We also could be implementing a state machine for an eletronic door which should validate the passcode to unlock it. In this scenario the `machinist` give us the possibility to provide a function to evaluate a condition and return the new state.

Check out the diagram below representing it:

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

In order to have this condition to the `unlock` event use the `event` macro passing the `guard` option with a one-arity function:
And in order to have this condition for the `unlock` event use the `event` macro passing the `guard` option with a one-arity function:

```elixir
# ..
Expand All @@ -106,7 +108,7 @@ defp check_passcode(door) do
end
```

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 parameter and it will return the new state to be set.
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.

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

Expand Down

0 comments on commit 9a33567

Please sign in to comment.