simple_state_machine 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/VERSION +1 -1
- data/lib/simple_state_machine/simple_state_machine.rb +8 -4
- data/simple_state_machine.gemspec +4 -3
- data/spec/simple_state_machine_spec.rb +20 -0
- metadata +5 -4
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.swp
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
@@ -4,7 +4,7 @@ module SimpleStateMachine
|
|
4
4
|
end
|
5
5
|
|
6
6
|
##
|
7
|
-
# Adds state machine methods to extended class
|
7
|
+
# Adds state machine methods to the extended class
|
8
8
|
module StateMachineMixin
|
9
9
|
|
10
10
|
# mark the method as an event and specify how the state should transition
|
@@ -53,7 +53,7 @@ module SimpleStateMachine
|
|
53
53
|
transitions << transition
|
54
54
|
transition
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
def state_method
|
58
58
|
@state_method ||= :state
|
59
59
|
end
|
@@ -126,11 +126,13 @@ module SimpleStateMachine
|
|
126
126
|
|
127
127
|
# override with your own implementation, like setting errors in your model
|
128
128
|
def illegal_event_callback event_name
|
129
|
-
raise Error.new("You cannot '#{event_name}' when state is '#{@subject.
|
129
|
+
raise Error.new("You cannot '#{event_name}' when state is '#{@subject.send(state_method)}'")
|
130
130
|
end
|
131
131
|
|
132
132
|
end
|
133
133
|
|
134
|
+
##
|
135
|
+
# Defines transitions for events
|
134
136
|
class Transition
|
135
137
|
attr_reader :event_name, :from, :to
|
136
138
|
def initialize(event_name, from, to)
|
@@ -139,17 +141,19 @@ module SimpleStateMachine
|
|
139
141
|
@to = to.to_s
|
140
142
|
end
|
141
143
|
|
144
|
+
# returns true if it's a transition for event_name
|
142
145
|
def is_transition_for?(event_name)
|
143
146
|
self.event_name == event_name.to_s
|
144
147
|
end
|
145
148
|
|
149
|
+
# returns true if it's a error transition for event_name and error
|
146
150
|
def is_error_transition_for?(event_name, error)
|
147
151
|
is_transition_for?(event_name) && error.class == from
|
148
152
|
end
|
149
153
|
end
|
150
154
|
|
151
155
|
##
|
152
|
-
# Decorates
|
156
|
+
# Decorates @subject with methods to access the state machine
|
153
157
|
class Decorator
|
154
158
|
|
155
159
|
def initialize(subject)
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{simple_state_machine}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Marek de Heus", "Petrik de Heus"]
|
12
|
-
s.date = %q{2010-09-
|
12
|
+
s.date = %q{2010-09-10}
|
13
13
|
s.description = %q{A simple DSL to decorate existing methods with logic that guards state transitions.}
|
14
14
|
s.email = ["FIX@example.com"]
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
"
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
21
22
|
"README.rdoc",
|
22
23
|
"Rakefile",
|
23
24
|
"VERSION",
|
@@ -14,6 +14,17 @@ class SimpleExample
|
|
14
14
|
event :event2, :state2 => :state3
|
15
15
|
end
|
16
16
|
|
17
|
+
class SimpleExampleWithCustomStateMethod
|
18
|
+
extend SimpleStateMachine
|
19
|
+
state_machine_definition.state_method = :ssm_state
|
20
|
+
|
21
|
+
def initialize(state = 'state1')
|
22
|
+
@ssm_state = state
|
23
|
+
end
|
24
|
+
event :event1, :state1 => :state2
|
25
|
+
event :event2, :state2 => :state3
|
26
|
+
end
|
27
|
+
|
17
28
|
describe SimpleStateMachine do
|
18
29
|
|
19
30
|
it "has an error that extends RuntimeError" do
|
@@ -76,6 +87,15 @@ describe SimpleStateMachine do
|
|
76
87
|
|
77
88
|
end
|
78
89
|
|
90
|
+
describe 'custom state method' do
|
91
|
+
|
92
|
+
it "raise an error if an invalid state_transition is called" do
|
93
|
+
example = SimpleExampleWithCustomStateMethod.new
|
94
|
+
lambda { example.event2 }.should raise_error(SimpleStateMachine::Error, "You cannot 'event2' when state is 'state1'")
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
79
99
|
describe "state_machine_definition" do
|
80
100
|
it "is inherited by subclasses" do
|
81
101
|
example = Class.new(SimpleExample).new
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Marek de Heus
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-09-
|
19
|
+
date: 2010-09-10 00:00:00 +02:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -46,6 +46,7 @@ extra_rdoc_files:
|
|
46
46
|
- LICENSE
|
47
47
|
- README.rdoc
|
48
48
|
files:
|
49
|
+
- .gitignore
|
49
50
|
- LICENSE
|
50
51
|
- README.rdoc
|
51
52
|
- Rakefile
|