simpler_state_machine 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -96,7 +96,7 @@ module SimplerStateMachine
|
|
96
96
|
def add_transition(transition_name, options = {})
|
97
97
|
|
98
98
|
# Convert from states to array
|
99
|
-
options[:from] = options[:from].is_a?(Array) ? options[:from] : options[:from]
|
99
|
+
options[:from] = options[:from].is_a?(Array) ? options[:from] : [options[:from]]
|
100
100
|
|
101
101
|
# Register transactions
|
102
102
|
@transitions[transition_name.to_s] = options
|
@@ -31,7 +31,7 @@ module SimplerStateMachine
|
|
31
31
|
# Create or return a state machine
|
32
32
|
def state_machine(&block)
|
33
33
|
return @sm if !(@sm.nil?) && !(block_given?)
|
34
|
-
@sm ||=
|
34
|
+
@sm ||= SimplerStateMachine::Base.new(self)
|
35
35
|
@sm.instance_eval(&block) if block_given?
|
36
36
|
@sm.enumize!
|
37
37
|
self.const_set(@sm.states_constant_name,@sm.states).freeze
|
data/spec/models/gate.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'ruby-debug'
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe SimplerStateMachine do
|
5
5
|
describe "- Normal state machine" do
|
6
6
|
before(:each) do
|
7
7
|
@gate = Gate.new
|
@@ -11,9 +11,9 @@ describe SimpleStateMachine do
|
|
11
11
|
describe "- Basic operations" do
|
12
12
|
|
13
13
|
it "Should allow accessible methods to check if certain state is active on the including class" do
|
14
|
-
@gate.methods.should include(
|
15
|
-
@gate.methods.should include(
|
16
|
-
@gate.methods.should include(
|
14
|
+
@gate.methods.should include(:beginner?)
|
15
|
+
@gate.methods.should include(:novice?)
|
16
|
+
@gate.methods.should include(:expert?)
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should have a state_machine reference" do
|
@@ -43,7 +43,7 @@ describe SimpleStateMachine do
|
|
43
43
|
it "should allow a gate to transition from 'beginner' to 'novice', normal method" do
|
44
44
|
lambda {
|
45
45
|
@gate.make_novice.should be_true
|
46
|
-
}.should_not raise_error(
|
46
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
47
47
|
|
48
48
|
@gate.novice?.should be_true
|
49
49
|
end
|
@@ -51,7 +51,7 @@ describe SimpleStateMachine do
|
|
51
51
|
it "should allow a gate to transition from 'beginner' to 'novice', with shebang" do
|
52
52
|
lambda {
|
53
53
|
@gate.make_novice!
|
54
|
-
}.should_not raise_error(
|
54
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
55
55
|
|
56
56
|
@gate.novice?.should be_true
|
57
57
|
end
|
@@ -59,7 +59,7 @@ describe SimpleStateMachine do
|
|
59
59
|
it "should not allow a gate to transition from 'beginner' to 'expert'" do
|
60
60
|
lambda {
|
61
61
|
@gate.make_expert.should be_false
|
62
|
-
}.should_not raise_error(
|
62
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
63
63
|
|
64
64
|
@gate.beginner?.should be_true
|
65
65
|
end
|
@@ -67,7 +67,7 @@ describe SimpleStateMachine do
|
|
67
67
|
it "should raise an exception if a gate tries to transition from 'beginner' to 'expert' with a shebang" do
|
68
68
|
lambda {
|
69
69
|
@gate.make_expert!
|
70
|
-
}.should raise_error(
|
70
|
+
}.should raise_error(SimplerStateMachine::Exceptions::InvalidTransition, "Could not transit 'beginner' to 'expert'")
|
71
71
|
|
72
72
|
@gate.beginner?.should be_true
|
73
73
|
end
|
@@ -76,7 +76,7 @@ describe SimpleStateMachine do
|
|
76
76
|
@gate.status = Gate.state_machine.states["expert"]
|
77
77
|
lambda {
|
78
78
|
@gate.make_novice.should be_true
|
79
|
-
}.should_not raise_error(
|
79
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
80
80
|
|
81
81
|
@gate.novice?.should be_true
|
82
82
|
end
|
@@ -85,7 +85,7 @@ describe SimpleStateMachine do
|
|
85
85
|
@gate.status = Gate.state_machine.states["expert"]
|
86
86
|
lambda {
|
87
87
|
@gate.make_novice!
|
88
|
-
}.should_not raise_error(
|
88
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
89
89
|
|
90
90
|
@gate.novice?.should be_true
|
91
91
|
end
|
@@ -133,7 +133,7 @@ describe SimpleStateMachine do
|
|
133
133
|
|
134
134
|
describe "- Basic operations" do
|
135
135
|
it "Should allow accessible constants on the including class" do
|
136
|
-
ExpressGate.constants.should include(
|
136
|
+
ExpressGate.constants.should include(:STATES)
|
137
137
|
ExpressGate::STATES["beginner"].should_not be_nil
|
138
138
|
ExpressGate::STATES["novice"].should_not be_nil
|
139
139
|
ExpressGate::STATES["expert"].should_not be_nil
|
@@ -143,15 +143,15 @@ describe SimpleStateMachine do
|
|
143
143
|
end
|
144
144
|
|
145
145
|
it "Should allow accessible methods to check if certain state is active on the including class for parent class' states" do
|
146
|
-
@gate.methods.should include(
|
147
|
-
@gate.methods.should include(
|
148
|
-
@gate.methods.should include(
|
146
|
+
@gate.methods.should include(:beginner?)
|
147
|
+
@gate.methods.should include(:novice?)
|
148
|
+
@gate.methods.should include(:expert?)
|
149
149
|
end
|
150
150
|
|
151
151
|
it "Should allow accessible methods to check if certain state is active on the including class for child class' states" do
|
152
|
-
@gate.methods.should include(
|
153
|
-
@gate.methods.should include(
|
154
|
-
@gate.methods.should include(
|
152
|
+
@gate.methods.should include(:express_beginner?)
|
153
|
+
@gate.methods.should include(:express_expert?)
|
154
|
+
@gate.methods.should include(:miki_level?)
|
155
155
|
end
|
156
156
|
|
157
157
|
it "should have a state_machine reference" do
|
@@ -182,7 +182,7 @@ describe SimpleStateMachine do
|
|
182
182
|
lambda {
|
183
183
|
@gate.status = ExpressGate.state_machine.states["expert"]
|
184
184
|
@gate.make_express_beginner.should be_true
|
185
|
-
}.should_not raise_error(
|
185
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
186
186
|
|
187
187
|
@gate.express_beginner?.should be_true
|
188
188
|
end
|
@@ -191,7 +191,7 @@ describe SimpleStateMachine do
|
|
191
191
|
lambda {
|
192
192
|
@gate.status = ExpressGate.state_machine.states["expert"]
|
193
193
|
@gate.make_express_beginner!
|
194
|
-
}.should_not raise_error(
|
194
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
195
195
|
|
196
196
|
@gate.express_beginner?.should be_true
|
197
197
|
end
|
@@ -200,7 +200,7 @@ describe SimpleStateMachine do
|
|
200
200
|
@gate.status = ExpressGate.state_machine.states["beginner"]
|
201
201
|
lambda {
|
202
202
|
@gate.make_express_beginner.should be_false
|
203
|
-
}.should_not raise_error(
|
203
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
204
204
|
|
205
205
|
@gate.beginner?.should be_true
|
206
206
|
end
|
@@ -209,7 +209,7 @@ describe SimpleStateMachine do
|
|
209
209
|
@gate.status = ExpressGate.state_machine.states["beginner"]
|
210
210
|
lambda {
|
211
211
|
@gate.make_express_beginner!
|
212
|
-
}.should raise_error(
|
212
|
+
}.should raise_error(SimplerStateMachine::Exceptions::InvalidTransition, "Could not transit 'beginner' to 'express_beginner'")
|
213
213
|
|
214
214
|
@gate.beginner?.should be_true
|
215
215
|
end
|
@@ -217,7 +217,7 @@ describe SimpleStateMachine do
|
|
217
217
|
it "should allow a gate to transition from 'express_beginner' to 'express_expert', normal method" do
|
218
218
|
lambda {
|
219
219
|
@gate.make_express_expert.should be_true
|
220
|
-
}.should_not raise_error(
|
220
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
221
221
|
|
222
222
|
@gate.express_expert?.should be_true
|
223
223
|
end
|
@@ -225,7 +225,7 @@ describe SimpleStateMachine do
|
|
225
225
|
it "should allow a gate to transition from 'express_beginner' to 'express_expert', with shebang" do
|
226
226
|
lambda {
|
227
227
|
@gate.make_express_expert!
|
228
|
-
}.should_not raise_error(
|
228
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
229
229
|
|
230
230
|
@gate.express_expert?.should be_true
|
231
231
|
end
|
@@ -233,7 +233,7 @@ describe SimpleStateMachine do
|
|
233
233
|
it "should not allow a gate to transition from 'express_beginner' to 'miki_level'" do
|
234
234
|
lambda {
|
235
235
|
@gate.make_miki.should be_false
|
236
|
-
}.should_not raise_error(
|
236
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
237
237
|
|
238
238
|
@gate.express_beginner?.should be_true
|
239
239
|
end
|
@@ -241,7 +241,7 @@ describe SimpleStateMachine do
|
|
241
241
|
it "should raise an exception if a gate tries to transition from 'express_beginner' to 'miki_level' with a shebang" do
|
242
242
|
lambda {
|
243
243
|
@gate.make_miki!
|
244
|
-
}.should raise_error(
|
244
|
+
}.should raise_error(SimplerStateMachine::Exceptions::InvalidTransition, "Could not transit 'express_beginner' to 'miki_level'")
|
245
245
|
|
246
246
|
@gate.express_beginner?.should be_true
|
247
247
|
end
|
@@ -250,7 +250,7 @@ describe SimpleStateMachine do
|
|
250
250
|
@gate.status = ExpressGate.state_machine.states["express_beginner"]
|
251
251
|
lambda {
|
252
252
|
@gate.demote_express_beginner.should be_true
|
253
|
-
}.should_not raise_error(
|
253
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
254
254
|
@gate.expert?.should be_true
|
255
255
|
end
|
256
256
|
|
@@ -258,7 +258,7 @@ describe SimpleStateMachine do
|
|
258
258
|
@gate.status = ExpressGate.state_machine.states["express_beginner"]
|
259
259
|
lambda {
|
260
260
|
@gate.demote_express_beginner!
|
261
|
-
}.should_not raise_error(
|
261
|
+
}.should_not raise_error(SimplerStateMachine::Exceptions::InvalidTransition)
|
262
262
|
|
263
263
|
@gate.expert?.should be_true
|
264
264
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simpler_state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
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: 2013-
|
12
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|