micromachine 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +20 -10
- data/lib/micromachine.rb +8 -0
- data/micromachine.gemspec +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -11,7 +11,7 @@ all provide a nice DSL for declaring events, exceptions, callbacks,
|
|
11
11
|
and all kinds of niceties in general.
|
12
12
|
|
13
13
|
But if all you want is a finite state machine, look no further: this
|
14
|
-
|
14
|
+
has less than 50 lines of code and provides everything a finite state
|
15
15
|
machine must have, and nothing more.
|
16
16
|
|
17
17
|
Usage
|
@@ -22,9 +22,10 @@ require 'micromachine'
|
|
22
22
|
|
23
23
|
machine = MicroMachine.new(:new) # Initial state.
|
24
24
|
|
25
|
-
|
26
|
-
machine.
|
27
|
-
machine.
|
25
|
+
# Define the possible transitions for each event.
|
26
|
+
machine.when(:confirm, :new => :confirmed)
|
27
|
+
machine.when(:ignore, :new => :ignored)
|
28
|
+
machine.when(:reset, :confirmed => :new, :ignored => :new)
|
28
29
|
|
29
30
|
machine.trigger(:confirm) #=> true
|
30
31
|
machine.state #=> :confirmed
|
@@ -39,6 +40,15 @@ machine.trigger(:ignore) #=> true
|
|
39
40
|
machine.state #=> :ignored
|
40
41
|
```
|
41
42
|
|
43
|
+
The `when` helper is syntactic sugar for assigning to the
|
44
|
+
`transitions_for` hash. This code is equivalent:
|
45
|
+
|
46
|
+
``` ruby
|
47
|
+
machine.transitions_for[:confirm] = { :new => :confirmed }
|
48
|
+
machine.transitions_for[:ignore] = { :new => :ignored }
|
49
|
+
machine.transitions_for[:reset] = { :confirmed => :new, :ignored => :new }
|
50
|
+
```
|
51
|
+
|
42
52
|
You can also ask if an event will trigger a change in state. Following
|
43
53
|
the example above:
|
44
54
|
|
@@ -103,9 +113,9 @@ class Event < ActiveRecord::Base
|
|
103
113
|
@confirmation ||= begin
|
104
114
|
fsm = MicroMachine.new(confirmation_state || "pending")
|
105
115
|
|
106
|
-
fsm.
|
107
|
-
fsm.
|
108
|
-
fsm.
|
116
|
+
fsm.when(:confirm, "pending" => "confirmed")
|
117
|
+
fsm.when(:cancel, "confirmed" => "cancelled")
|
118
|
+
fsm.when(:reset, "confirmed" => "pending", "cancelled" => "pending")
|
109
119
|
|
110
120
|
fsm
|
111
121
|
end
|
@@ -143,9 +153,9 @@ class Event < ActiveRecord::Base
|
|
143
153
|
@confirmation ||= begin
|
144
154
|
fsm = MicroMachine.new(confirmation_state || "pending")
|
145
155
|
|
146
|
-
fsm.
|
147
|
-
fsm.
|
148
|
-
fsm.
|
156
|
+
fsm.when(:confirm, "pending" => "confirmed")
|
157
|
+
fsm.when(:cancel, "confirmed" => "cancelled")
|
158
|
+
fsm.when(:reset, "confirmed" => "pending", "cancelled" => "pending")
|
149
159
|
|
150
160
|
fsm.on(:any) { self.confirmation_state = confirmation.state }
|
151
161
|
|
data/lib/micromachine.rb
CHANGED
@@ -14,6 +14,10 @@ class MicroMachine
|
|
14
14
|
@callbacks[key] << block
|
15
15
|
end
|
16
16
|
|
17
|
+
def when(event, transitions)
|
18
|
+
transitions_for[event] = transitions
|
19
|
+
end
|
20
|
+
|
17
21
|
def trigger event
|
18
22
|
if trigger?(event)
|
19
23
|
@state = transitions_for[event][@state]
|
@@ -31,6 +35,10 @@ class MicroMachine
|
|
31
35
|
raise InvalidEvent
|
32
36
|
end
|
33
37
|
|
38
|
+
def events
|
39
|
+
transitions_for.keys
|
40
|
+
end
|
41
|
+
|
34
42
|
def ==(some_state)
|
35
43
|
state == some_state
|
36
44
|
end
|
data/micromachine.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'micromachine'
|
3
|
-
s.version = '1.0.
|
3
|
+
s.version = '1.0.1'
|
4
4
|
s.summary = %{Minimal Finite State Machine.}
|
5
5
|
s.description = %Q{There are many finite state machine implementations for Ruby, and they all provide a nice DSL for declaring events, exceptions, callbacks, and all kinds of niceties in general.\n\nBut if all you want is a finite state machine, look no further: this is only 22 lines of code and provides everything a finite state machine must have, and nothing more.}
|
6
6
|
s.author = "Michel Martens"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micromachine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-29 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'There are many finite state machine implementations for Ruby, and
|
15
15
|
they all provide a nice DSL for declaring events, exceptions, callbacks, and all
|