hifsm 0.4.1 → 0.4.3
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.
- checksums.yaml +4 -4
- data/lib/hifsm/fsm.rb +41 -25
- data/lib/hifsm/machine.rb +9 -1
- data/lib/hifsm/state.rb +5 -5
- data/lib/hifsm/version.rb +1 -1
- data/lib/hifsm.rb +0 -8
- data/test/test_activerecord_adapter.rb +1 -1
- data/test/test_any_state_event.rb +6 -2
- data/test/test_basic_fsm.rb +3 -3
- data/test/test_hierarchical.rb +26 -6
- data/test/test_two_machines.rb +3 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2036f5f0392c1b5556c67030d1f3f4124355b725
|
4
|
+
data.tar.gz: ee74be4931ceb43693bb6e32dae2cb320e6ff848
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f48f8b7c4967ad34beb03b74057dc0c876df9a855b3c23194ea6a2a3fd74945884f5ef7f265d42a2c874dcd0b39c270b47d80125e63a0ad801d49c329306f3c7
|
7
|
+
data.tar.gz: 6b49eac8561505a6a7a10d20024abe58c94c677b936e8f5ccf8fdff82f7c2581ca51c4dc49a5a0651e24e1a7485c36ee50781bd290de96dfe34f5d0008ac8249
|
data/lib/hifsm/fsm.rb
CHANGED
@@ -17,10 +17,10 @@ module Hifsm
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def instantiate(target = nil, initial_state = nil)
|
20
|
-
|
20
|
+
machine_module = get_machine_module
|
21
21
|
machine_name = "#{name}_machine"
|
22
22
|
@machine_class ||= Class.new(Hifsm::Machine) do
|
23
|
-
include
|
23
|
+
include machine_module
|
24
24
|
define_method(machine_name) { self }
|
25
25
|
end
|
26
26
|
@machine_class.new(self, target, initial_state)
|
@@ -81,35 +81,51 @@ module Hifsm
|
|
81
81
|
end
|
82
82
|
|
83
83
|
private
|
84
|
-
def
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
84
|
+
def get_machine_module
|
85
|
+
fsm = self # capture self
|
86
|
+
machine_name = "#{name}_machine"
|
87
|
+
@machine_module ||= Module.new do
|
88
|
+
# <event> fires event
|
89
|
+
fsm.all_events.each do |event_name, event|
|
90
|
+
define_method(event_name) {|*args| send(machine_name).fire(event_name, *args) }
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
94
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
95
|
+
def get_fsm_module
|
96
|
+
fsm = self # capture self
|
97
|
+
machine_var = "@#{name}_machine"
|
98
|
+
machine_name = "#{name}_machine"
|
99
|
+
machine_module = get_machine_module
|
100
|
+
@fsm_module ||= Module.new do
|
101
|
+
include machine_module
|
102
|
+
|
103
|
+
define_singleton_method :included do |base|
|
104
|
+
base.class_eval do
|
105
|
+
define_singleton_method("#{machine_name}_definition") { fsm }
|
106
|
+
|
107
|
+
# act!
|
108
|
+
define_method "act_with_#{machine_name}!" do |*args|
|
109
|
+
send("act_without_#{machine_name}!", *args) if respond_to?("act_without_#{machine_name}!")
|
110
|
+
send("#{machine_name}").act!(*args)
|
102
111
|
end
|
112
|
+
alias_method "act_without_#{machine_name}!", :act! if method_defined?(:act!)
|
113
|
+
alias_method :act!, "act_with_#{machine_name}!"
|
103
114
|
end
|
115
|
+
end
|
104
116
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
117
|
+
# <state>_machine returns machine instance
|
118
|
+
define_method(machine_name) do
|
119
|
+
if instance_variable_defined?(machine_var)
|
120
|
+
instance_variable_get(machine_var)
|
121
|
+
else
|
122
|
+
machine = fsm.instantiate(self)
|
123
|
+
instance_variable_set(machine_var, machine)
|
111
124
|
end
|
112
125
|
end
|
126
|
+
|
127
|
+
# <state> returns string representation of the current state
|
128
|
+
define_method(fsm.name) { send(machine_name).to_s }
|
113
129
|
end
|
114
130
|
end
|
115
131
|
end
|
data/lib/hifsm/machine.rb
CHANGED
data/lib/hifsm/state.rb
CHANGED
@@ -2,10 +2,10 @@ module Hifsm
|
|
2
2
|
class State
|
3
3
|
CALLBACKS = [:before_enter, :before_exit, :after_enter, :after_exit, :action].freeze
|
4
4
|
|
5
|
-
attr_reader :sub_fsm
|
5
|
+
attr_reader :name, :sub_fsm
|
6
6
|
|
7
7
|
def initialize(name, parent = nil, options)
|
8
|
-
@name = name
|
8
|
+
@name = name.to_s
|
9
9
|
@parent = parent
|
10
10
|
@callbacks = {}
|
11
11
|
CALLBACKS.each do |cb|
|
@@ -34,7 +34,7 @@ module Hifsm
|
|
34
34
|
|
35
35
|
def enter!
|
36
36
|
if @sub_fsm
|
37
|
-
@sub_fsm.initial_state!
|
37
|
+
@sub_fsm.initial_state!.enter!
|
38
38
|
else
|
39
39
|
self
|
40
40
|
end
|
@@ -78,9 +78,9 @@ module Hifsm
|
|
78
78
|
|
79
79
|
def to_s
|
80
80
|
if @parent
|
81
|
-
"#{@parent.to_s}.#{@name
|
81
|
+
"#{@parent.to_s}.#{@name}"
|
82
82
|
else
|
83
|
-
@name
|
83
|
+
@name
|
84
84
|
end
|
85
85
|
end
|
86
86
|
end
|
data/lib/hifsm/version.rb
CHANGED
data/lib/hifsm.rb
CHANGED
@@ -28,14 +28,6 @@ module Hifsm
|
|
28
28
|
module ClassMethods
|
29
29
|
def hifsm(name = :state, &block)
|
30
30
|
include FSM::new(name, &block).to_module
|
31
|
-
|
32
|
-
# act!
|
33
|
-
define_method("act_with_#{name}_machine!") do |*args|
|
34
|
-
send("act_without_#{name}_machine!", *args) if respond_to?("act_without_#{name}_machine!")
|
35
|
-
send("#{name}_machine").act!(*args)
|
36
|
-
end
|
37
|
-
alias_method "act_without_#{name}_machine!", :act! if method_defined?(:act!)
|
38
|
-
alias_method :act!, "act_with_#{name}_machine!"
|
39
31
|
end
|
40
32
|
end
|
41
33
|
end
|
@@ -48,7 +48,7 @@ class TestActiverecrodAdapter < Minitest::Test
|
|
48
48
|
|
49
49
|
def test_new_machines_saved_in_initial_state
|
50
50
|
@machine = SodaMachine.create
|
51
|
-
assert_equal 'off', @machine.state_machine.state
|
51
|
+
assert_equal 'off', @machine.state_machine.state.to_s
|
52
52
|
assert_equal 'off', @machine.state
|
53
53
|
end
|
54
54
|
|
@@ -16,13 +16,17 @@ class TestAnyStateEvent < Minitest::Test
|
|
16
16
|
|
17
17
|
def test_halt_from_off
|
18
18
|
@machine.halt
|
19
|
-
assert_equal 'halt', @machine.state
|
19
|
+
assert_equal 'halt', @machine.state.to_s
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_halt_from_on
|
23
23
|
@machine.toggle
|
24
24
|
@machine.halt
|
25
|
-
assert_equal 'halt', @machine.state
|
25
|
+
assert_equal 'halt', @machine.state.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_event_is_added_to_states
|
29
|
+
assert_equal ['toggle', 'halt'], @machine.state.events
|
26
30
|
end
|
27
31
|
|
28
32
|
end
|
data/test/test_basic_fsm.rb
CHANGED
@@ -13,18 +13,18 @@ class TestBasicFSM < Minitest::Test
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_initial_state_is_off
|
16
|
-
assert_equal 'off', @machine.state
|
16
|
+
assert_equal 'off', @machine.state.to_s
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_toggle_switches_state_to_on
|
20
20
|
@machine.toggle
|
21
|
-
assert_equal 'on', @machine.state
|
21
|
+
assert_equal 'on', @machine.state.to_s
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_toggle_twice_switches_state_back_to_off
|
25
25
|
@machine.toggle
|
26
26
|
@machine.toggle
|
27
|
-
assert_equal 'off', @machine.state
|
27
|
+
assert_equal 'off', @machine.state.to_s
|
28
28
|
end
|
29
29
|
|
30
30
|
def test_instantiating_maching_in_unknown_state_raises_error
|
data/test/test_hierarchical.rb
CHANGED
@@ -5,7 +5,15 @@ class TestHierarchical < Minitest::Test
|
|
5
5
|
@fsm = Hifsm::FSM.new do
|
6
6
|
async = proc do
|
7
7
|
state :pending, :initial => true
|
8
|
-
state :sync
|
8
|
+
state :sync do
|
9
|
+
state :third_level, :initial => true
|
10
|
+
state :third_level_two
|
11
|
+
|
12
|
+
event :switch_levels do
|
13
|
+
from :third_level, :to => :third_level_two
|
14
|
+
from :third_level_two, :to => :third_level
|
15
|
+
end
|
16
|
+
end
|
9
17
|
|
10
18
|
event :sync, :from => :pending, :to => :sync
|
11
19
|
end
|
@@ -20,12 +28,12 @@ class TestHierarchical < Minitest::Test
|
|
20
28
|
|
21
29
|
def test_initial_state_is_off_pending_by_default
|
22
30
|
machine = @fsm.instantiate
|
23
|
-
assert_equal 'off.pending', machine.state
|
31
|
+
assert_equal 'off.pending', machine.state.to_s
|
24
32
|
end
|
25
33
|
|
26
34
|
def test_explicit_initial_state
|
27
35
|
machine2 = @fsm.instantiate(nil, 'on.sync')
|
28
|
-
assert_equal 'on.sync', machine2.state
|
36
|
+
assert_equal 'on.sync.third_level', machine2.state.to_s
|
29
37
|
machine2.toggle
|
30
38
|
pass # assert_nothing_raised
|
31
39
|
end
|
@@ -40,19 +48,31 @@ class TestHierarchical < Minitest::Test
|
|
40
48
|
def test_sync
|
41
49
|
machine = @fsm.instantiate
|
42
50
|
machine.sync
|
43
|
-
assert_equal 'off.sync', machine.state
|
51
|
+
assert_equal 'off.sync.third_level', machine.state.to_s
|
44
52
|
end
|
45
53
|
|
46
54
|
def test_toggle_from_off_sync_to_on_pending
|
47
55
|
machine = @fsm.instantiate
|
48
56
|
machine.sync
|
49
57
|
machine.toggle
|
50
|
-
assert_equal 'on.pending', machine.state
|
58
|
+
assert_equal 'on.pending', machine.state.to_s
|
51
59
|
end
|
52
60
|
|
53
61
|
def test_toggle_from_on_sync_to_off_sync
|
54
62
|
machine2 = @fsm.instantiate(nil, 'on.sync')
|
55
63
|
machine2.toggle
|
56
|
-
assert_equal '
|
64
|
+
assert_equal 'third_level', machine2.state.name
|
65
|
+
assert_equal 'off.sync.third_level', machine2.state.to_s
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_machine_states
|
69
|
+
machine = @fsm.instantiate
|
70
|
+
assert_equal ["off", "on"], machine.states
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_machine_all_states
|
74
|
+
machine = @fsm.instantiate
|
75
|
+
# note the all_states do not include states from test_machine_states
|
76
|
+
assert_equal ["off.pending", "off.sync.third_level", "off.sync.third_level_two", "on.pending", "on.sync.third_level", "on.sync.third_level_two"], machine.all_states
|
57
77
|
end
|
58
78
|
end
|
data/test/test_two_machines.rb
CHANGED
@@ -38,8 +38,9 @@ class TestTwoMachines < Minitest::Test
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def test_two_machines_defined
|
41
|
-
assert_equal 'off', @color_printer.working_state_machine.state
|
42
|
-
assert_equal 'red', @color_printer.color_machine.
|
41
|
+
assert_equal 'off', @color_printer.working_state_machine.state.to_s
|
42
|
+
assert_equal 'red', @color_printer.color_machine.state.to_s
|
43
|
+
assert_equal 'red', @color_printer.color # alias for color_mathine.state.to_s
|
43
44
|
end
|
44
45
|
|
45
46
|
def test_initial_state_is_off_and_red
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hifsm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Meremyanin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|