big_machine 1.0.1 → 1.1.0
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/lib/big_machine/active_record.rb +0 -4
- data/lib/big_machine/state.rb +30 -29
- data/lib/big_machine/version.rb +1 -1
- data/lib/big_machine.rb +13 -3
- data/test/big_machine_test.rb +63 -1
- metadata +1 -1
data/lib/big_machine/state.rb
CHANGED
@@ -1,29 +1,30 @@
|
|
1
|
-
module BigMachine
|
2
|
-
class State
|
3
|
-
|
4
|
-
attr_reader :stateful
|
5
|
-
|
6
|
-
def initialize(stateful)
|
7
|
-
@stateful = stateful
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
def exit
|
27
|
-
|
28
|
-
|
29
|
-
end
|
1
|
+
module BigMachine
|
2
|
+
class State
|
3
|
+
|
4
|
+
attr_reader :stateful
|
5
|
+
|
6
|
+
def initialize(stateful)
|
7
|
+
@stateful = stateful
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.transition_methods
|
11
|
+
public_instance_methods - State.public_instance_methods
|
12
|
+
end
|
13
|
+
|
14
|
+
def transition_to(state_class, *args, &block)
|
15
|
+
@stateful.transition_to(state_class, *args, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def workflow_is(name)
|
19
|
+
@stateful.workflow == name
|
20
|
+
end
|
21
|
+
|
22
|
+
def enter(*args, &block)
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def exit(*args, &block)
|
27
|
+
true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/big_machine/version.rb
CHANGED
data/lib/big_machine.rb
CHANGED
@@ -18,7 +18,7 @@ module BigMachine
|
|
18
18
|
end
|
19
19
|
|
20
20
|
module ClassMethods
|
21
|
-
def active_record_model?
|
21
|
+
def active_record_model?
|
22
22
|
defined?(::ActiveRecord::Base) && self.ancestors.include?(::ActiveRecord::Base)
|
23
23
|
end
|
24
24
|
|
@@ -55,9 +55,19 @@ module BigMachine
|
|
55
55
|
def_delegators :current_state, *current_state.class.transition_methods
|
56
56
|
end
|
57
57
|
|
58
|
-
def transition_to(next_state_class)
|
59
|
-
current_state.exit
|
58
|
+
def transition_to(next_state_class, *args, &block)
|
59
|
+
return unless current_state.exit *args
|
60
|
+
|
61
|
+
previous_state = current_state
|
60
62
|
set_current_state next_state_class
|
63
|
+
|
64
|
+
rollback(previous_state) and return unless current_state.enter *args
|
65
|
+
|
66
|
+
block.call self if block_given?
|
67
|
+
end
|
68
|
+
|
69
|
+
def rollback(previous_state)
|
70
|
+
@current_state = @previous_state
|
61
71
|
end
|
62
72
|
|
63
73
|
end
|
data/test/big_machine_test.rb
CHANGED
@@ -7,11 +7,49 @@ class Draft < BigMachine::State
|
|
7
7
|
transition_to Online
|
8
8
|
end
|
9
9
|
|
10
|
+
def cannot_enter
|
11
|
+
transition_to CannotEnter
|
12
|
+
end
|
13
|
+
|
10
14
|
def lock
|
11
15
|
transition_to LockState
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
19
|
+
class Args < BigMachine::State
|
20
|
+
|
21
|
+
def publish
|
22
|
+
transition_to Online, 'args'
|
23
|
+
end
|
24
|
+
|
25
|
+
def back_to_draft
|
26
|
+
transition_to Online do |obj|
|
27
|
+
obj.block = 'block'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def exit(*args)
|
32
|
+
@stateful.args = args.first
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class CannotExit < BigMachine::State
|
38
|
+
|
39
|
+
def publish
|
40
|
+
transition_to Online
|
41
|
+
end
|
42
|
+
|
43
|
+
def exit
|
44
|
+
false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class CannotEnter < BigMachine::State
|
49
|
+
def enter
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
15
53
|
class LockState < BigMachine::State
|
16
54
|
include BigMachine::Lock
|
17
55
|
|
@@ -41,7 +79,7 @@ end
|
|
41
79
|
class DummyWithActiveRecord < ActiveRecord::Base
|
42
80
|
include BigMachine
|
43
81
|
|
44
|
-
attr_accessor :state
|
82
|
+
attr_accessor :state, :args, :block
|
45
83
|
|
46
84
|
big_machine initial_state: :draft
|
47
85
|
|
@@ -128,4 +166,28 @@ class BigMachineTest < ActiveSupport::TestCase
|
|
128
166
|
@dummyWS = DummyWithActiveRecord.new(nil)
|
129
167
|
assert_equal 'Draft', @dummyWS.current_state.class.name
|
130
168
|
end
|
169
|
+
|
170
|
+
test "big machine does not transtion if it's not possible to exit" do
|
171
|
+
@dummyCE = DummyWithActiveRecord.new('CannotExit')
|
172
|
+
@dummyCE.publish
|
173
|
+
assert_equal 'CannotExit', @dummyCE.current_state.class.name
|
174
|
+
end
|
175
|
+
|
176
|
+
test "big machine does not transtion if it's not possible to enter" do
|
177
|
+
@dummyCE = DummyWithActiveRecord.new('Draft')
|
178
|
+
@dummyCE.cannot_enter
|
179
|
+
assert_equal 'Draft', @dummyCE.current_state.class.name
|
180
|
+
end
|
181
|
+
|
182
|
+
test "big machine can take args into transition" do
|
183
|
+
@dummyArgs = DummyWithActiveRecord.new('Args')
|
184
|
+
@dummyArgs.publish
|
185
|
+
assert_equal 'args', @dummyArgs.args
|
186
|
+
end
|
187
|
+
|
188
|
+
test "big machine can take block into transition" do
|
189
|
+
@dummyArgs = DummyWithActiveRecord.new('Args')
|
190
|
+
@dummyArgs.back_to_draft
|
191
|
+
assert_equal 'block', @dummyArgs.block
|
192
|
+
end
|
131
193
|
end
|