minimal_state_machine 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/README.md +60 -10
- data/lib/minimal_state_machine/version.rb +1 -1
- data/lib/minimal_state_machine.rb +0 -1
- data/spec/minimal_state_machine_spec.rb +1 -0
- metadata +1 -2
- data/db/minimal_state_machine.sqlite3 +0 -0
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,24 +1,74 @@
|
|
1
1
|
# MinimalStateMachine
|
2
2
|
|
3
|
-
|
3
|
+
A state machine for ActiveRecord implemented with the state pattern
|
4
4
|
|
5
|
-
##
|
5
|
+
## Usage
|
6
6
|
|
7
|
-
Add
|
7
|
+
Add to Gemfile:
|
8
8
|
|
9
|
-
|
9
|
+
```ruby
|
10
|
+
gem 'minimal_state_machine'
|
11
|
+
```
|
10
12
|
|
11
|
-
|
13
|
+
Run:
|
12
14
|
|
13
|
-
|
15
|
+
```ruby
|
16
|
+
bundle install
|
17
|
+
rails generate minimal_state_machine
|
18
|
+
rake db:migrate
|
19
|
+
```
|
14
20
|
|
15
|
-
|
21
|
+
## Quick Start
|
16
22
|
|
17
|
-
|
23
|
+
Let's say we have an Issue ActiveRecord model and we want to turn it into a state machine with 3 possible states: open, closed, solved
|
18
24
|
|
19
|
-
|
25
|
+
```ruby
|
26
|
+
class Issue < ActiveRecord::Base
|
27
|
+
include MinimalStateMachine
|
28
|
+
|
29
|
+
def self.states
|
30
|
+
{ :open => IssueState::Open, :closed => IssueState::Closed, :solved => IssueState::Solved }
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.initial_state
|
34
|
+
'open'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class IssueState::Open < MinimalStateMachine::State
|
39
|
+
def self.valid_transition_states
|
40
|
+
%w(closed)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class IssueState::Closed < MinimalStateMachine::State
|
45
|
+
def self.valid_transition_states
|
46
|
+
%w(solved open)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class IssueState::Solved < MinimalStateMachine::State; end
|
51
|
+
```
|
52
|
+
|
53
|
+
As you can se we define the possible states with an Hash including both the state names and the classes representing the states.
|
54
|
+
We can provide an optional `initial_state` class method to indicate the initial state (the default value is the first of the keys in the states Hash)
|
55
|
+
|
56
|
+
What would happen with this configuration:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
Issue.new.state
|
60
|
+
=> #<IssueState::Open id: nil, type: "IssueState::Open", state_machine_id: nil, state_machine_type: "Issue", created_at: nil, updated_at: nil>
|
61
|
+
|
62
|
+
issue = Issue.new
|
63
|
+
issue.save
|
64
|
+
issue.state_name = 'closed'
|
65
|
+
issue.state
|
66
|
+
=> #<IssueState::Closed id: 1, type: "IssueState::Closed", state_machine_id: 1, state_machine_type: "Issue", created_at: "2013-01-03 19:13:31", updated_at: "2013-01-03 19:13:31">
|
67
|
+
```
|
68
|
+
|
69
|
+
If you try to change the state to a non valid state a `MinimalStateMachine::InvalidTransitionError` will be raised.
|
20
70
|
|
21
|
-
|
71
|
+
If you try to set the state to a non declared state a `MinimalStateMachine::InvalidStateError` will be raised.
|
22
72
|
|
23
73
|
## Contributing
|
24
74
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimal_state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -88,7 +88,6 @@ files:
|
|
88
88
|
- LICENSE.txt
|
89
89
|
- README.md
|
90
90
|
- Rakefile
|
91
|
-
- db/minimal_state_machine.sqlite3
|
92
91
|
- lib/generators/minimal_state_machine/minimal_state_machine_generator.rb
|
93
92
|
- lib/generators/minimal_state_machine/templates/create_msm_states.rb
|
94
93
|
- lib/minimal_state_machine.rb
|
Binary file
|