simplefsm 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simplefsm.rb +25 -32
- 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: f2a2b085fc53bb8bad80db05012653c5ec305808
|
4
|
+
data.tar.gz: 64d69c9349fff3f51c8ede74117b21596cf35afa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23e82bb0db0fa86ad0f754d2a018c07b1d56b0ea620a863fed0107d6f609f96837114e6da528edbb9398200323e3a4d70450a19552eb0c245199ca1c56386e14
|
7
|
+
data.tar.gz: 011724619fde75883c4dc803479e41280327690d36f1bf58c4ad99ac5f23ceea5803173357de1bd1ac09d9a51c0db7b0c0ffd124b732348fb1099a3d8a05cb40
|
data/lib/simplefsm.rb
CHANGED
@@ -14,7 +14,7 @@
|
|
14
14
|
# License:: MIT License
|
15
15
|
|
16
16
|
module SimpleFSM
|
17
|
-
VERSION = '0.2.
|
17
|
+
VERSION = '0.2.3'
|
18
18
|
|
19
19
|
# TransitionFactory instance is a temporary helper object
|
20
20
|
# for making transitions from a transitions_for code block parameter
|
@@ -35,16 +35,22 @@ module SimpleFSM
|
|
35
35
|
|
36
36
|
#Instance and class methods that are to be injected to the host class
|
37
37
|
def initialize
|
38
|
-
|
38
|
+
# set_current_state nil
|
39
|
+
# self.set_current_state {}
|
39
40
|
super
|
40
41
|
end
|
41
42
|
|
42
|
-
# start the machine
|
43
|
-
def run
|
44
|
-
|
45
|
-
if
|
46
|
-
|
43
|
+
# start the machine only if it hasn't been started
|
44
|
+
def run *args
|
45
|
+
st = current_state args
|
46
|
+
if !st
|
47
|
+
st = @@states.first
|
48
|
+
if st[:on]
|
49
|
+
self.send(st[:on][:enter], args) if st[:on].has_key?(:enter)
|
50
|
+
end
|
51
|
+
set_current_state(st, args)
|
47
52
|
end
|
53
|
+
st
|
48
54
|
end
|
49
55
|
|
50
56
|
# injecting the class methods for FSM definition
|
@@ -61,7 +67,6 @@ module SimpleFSM
|
|
61
67
|
# - one method is defined for every event specified
|
62
68
|
@@events.each do |ev|
|
63
69
|
Kernel.send :define_method, ev do |*args|
|
64
|
-
fsm_prepare_state args
|
65
70
|
|
66
71
|
if args
|
67
72
|
# If we have args here it must be an Array
|
@@ -72,14 +77,14 @@ module SimpleFSM
|
|
72
77
|
args = []
|
73
78
|
end
|
74
79
|
|
75
|
-
if current_state.class == Hash
|
76
|
-
st = current_state[:state]
|
80
|
+
if current_state(args).class == Hash
|
81
|
+
st = current_state(args)[:state]
|
77
82
|
else
|
78
|
-
st = current_state
|
83
|
+
st = current_state(args)
|
79
84
|
end
|
80
85
|
|
81
86
|
statetrans = @@transitions[st]
|
82
|
-
uniquestates = []
|
87
|
+
# uniquestates = []
|
83
88
|
|
84
89
|
if statetrans
|
85
90
|
# Get all transitions for this event in the current state
|
@@ -141,7 +146,6 @@ module SimpleFSM
|
|
141
146
|
end
|
142
147
|
end
|
143
148
|
end
|
144
|
-
fsm_save_state args
|
145
149
|
end
|
146
150
|
end
|
147
151
|
end
|
@@ -257,13 +261,13 @@ module SimpleFSM
|
|
257
261
|
end
|
258
262
|
|
259
263
|
# onexit, onenter = nil, nil
|
260
|
-
onexit = current_state[:on][:exit] if current_state.has_key?(:on) and current_state[:on] and current_state[:on].has_key?(:exit)
|
264
|
+
onexit = current_state(args)[:on][:exit] if current_state(args).has_key?(:on) and current_state(args)[:on] and current_state(args)[:on].has_key?(:exit)
|
261
265
|
if newstate[:on]
|
262
266
|
onenter = newstate[:on][:enter] if newstate[:on].has_key?(:enter)
|
263
267
|
end
|
264
268
|
|
265
269
|
self.send(onexit, args) if onexit
|
266
|
-
|
270
|
+
set_current_state newstate, args
|
267
271
|
self.send(onenter, args) if onenter
|
268
272
|
end
|
269
273
|
|
@@ -283,7 +287,7 @@ module SimpleFSM
|
|
283
287
|
ev = []
|
284
288
|
tr = @@transitions[st[:state]]
|
285
289
|
ev << tr.map{|tran| tran[:event]} if tr
|
286
|
-
ev.uniq!
|
290
|
+
ev.flatten!.uniq!
|
287
291
|
ev
|
288
292
|
end
|
289
293
|
|
@@ -297,31 +301,20 @@ module SimpleFSM
|
|
297
301
|
#
|
298
302
|
# #current_state is a private accessor that returns a full state object
|
299
303
|
# #state is a public method that returns only the sate's name
|
300
|
-
# #fsm_prepare_state method is called before and
|
301
|
-
# #fsm_save_state method is called after actual state transition and all consequent actions.
|
302
304
|
|
303
|
-
def current_state
|
305
|
+
def current_state *args
|
304
306
|
@state
|
305
307
|
end
|
306
308
|
|
307
|
-
def
|
309
|
+
def set_current_state(st, *args)
|
308
310
|
@state = st
|
309
311
|
end
|
310
312
|
|
311
|
-
def state
|
312
|
-
current_state[:state] #.to_sym
|
313
|
-
end
|
314
|
-
|
315
|
-
def fsm_prepare_state args
|
316
|
-
current_state
|
317
|
-
end
|
318
|
-
|
319
|
-
def fsm_save_state args
|
320
|
-
current_state
|
313
|
+
def state *args
|
314
|
+
current_state(args)[:state] #.to_sym
|
321
315
|
end
|
322
316
|
|
323
317
|
public :state
|
324
|
-
private :current_state, :
|
325
|
-
private :fsm_prepare_state, :fsm_save_state
|
318
|
+
private :current_state, :set_current_state
|
326
319
|
|
327
320
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simplefsm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Edin Pjanic
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-04-
|
12
|
+
date: 2017-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A simple and lightweight domain specific language (DSL) for modeling
|
15
15
|
finite state machines (FSM).
|