Skip to content

Commit b21d8cc

Browse files
authored
Merge pull request #5 from joaomdmoura/fixing-docs
Updating README and wrong docs
2 parents df7d381 + 415c79d commit b21d8cc

4 files changed

Lines changed: 51 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## WIP Version
44

5+
## 0.4.1
6+
- Updating wrong docs and README - [Pull Request](https://github.com/joaomdmoura/machinery/pull/3)
7+
58
## 0.4.0
69
- New, more functional DSL, not relying on Macros so much - [Pull Request](https://github.com/joaomdmoura/machinery/pull/1)
710
- Adding support for before and after callbacks - [Pull Request](https://github.com/joaomdmoura/machinery/pull/2)

README.md

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,28 @@ dependencies in `mix.exs`:
2828
```elixir
2929
def deps do
3030
[
31-
{:machinery, "~> 0.4.0"}
31+
{:machinery, "~> 0.4.1"}
3232
]
3333
end
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

4141
Machinery 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
4949
defmodule 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],
@@ -59,13 +60,15 @@ end
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`
6566
It 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
7174
Machinery.transition_to(your_struct, :next_state)
@@ -79,26 +82,27 @@ user = Accounts.get_user!(1)
7982
UserStateMachine.transition_to(user, :partial)
8083
```
8184

82-
Guard functions, before and after callbacks will be checked automatically.
83-
8485
## Guard functions
8586
Create guard conditions by adding signatures of the `guard_transition/2`
8687
function, pattern matching the desired state you want to guard.
8788

89+
```elixir
90+
def guard_transition(struct, :state), do: true
91+
```
92+
8893
Guard 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
93100
defmodule 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

110114
You 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
116131
defmodule 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
134148
end

lib/machinery.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
defmodule Machinery do
22
@moduledoc """
33
Main Machinery module.
4-
It keeps the bunk of the Machinery logics, it's the module that
5-
will be imported with `use` on the module that the state machine will
6-
be implemented.
4+
It keeps the bunk of the Machinery logics, it's the module that will be
5+
imported with `use` on the module that the state machine will be implemented.
6+
The first state declared will be considered the intial state
77
88
## Parameters
99
1010
- `opts`: A Keyword including `states` and `transitions`.
1111
- `states`: A List of Atoms representing each state.
12-
- `transitions`: A List of Maps, including two keys `from` and `to`, `to` might be an Atom or a List of Atoms.
12+
- `transitions`: A Map for each state and it allowed next state(s).
1313
1414
## Example
1515
```
1616
defmodule Project.User do
1717
use Machinery,
1818
states: [:created, :partial, :complete],
19-
transitions: [
20-
%{from: :created, to: [:partial, :complete]},
21-
%{from: :partal, to: :complete}
22-
]
19+
transitions: %{
20+
created: [:partial, :complete],
21+
partial: :completed
22+
}
2323
end
2424
```
2525
"""
@@ -54,9 +54,9 @@ defmodule Machinery do
5454
end
5555

5656
@doc """
57-
Triggers the transition of a struct to a new state if it passes the
57+
Triggers the transition of a struct to a new state, if it passes any
5858
existing guard clause, also runs any before or after callbacks.
59-
It returns a tuple with `{:ok, state}`, or `{:error, "cause"}`.
59+
It returns a tuple with `{:ok, struct}`, or `{:error, "reason"}`.
6060
6161
## Parameters
6262

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Machinery.Mixfile do
44
def project do
55
[
66
app: :machinery,
7-
version: "0.4.0",
7+
version: "0.4.1",
88
elixir: "~> 1.5",
99
start_permanent: Mix.env == :prod,
1010
deps: deps(),

0 commit comments

Comments
 (0)