microfsm 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +10 -0
- data/Gemfile.lock +4 -4
- data/README.md +37 -11
- data/lib/microfsm.rb +10 -2
- data/lib/version.rb +2 -1
- data/microfsm.gemspec +2 -2
- data/test/microfsm_test.rb +7 -0
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c13fcd2761ef9a4a28e56b5366bb9cde6a7e13a41014666fce63bd81b461445b
|
4
|
+
data.tar.gz: e95df3ce4f7f737d20a61b9ddb690eb5648eb3badfbd36ace291e2668db6a408
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bca81a60f2f226f0e8b5e321d66a223df846e561d274465db6a5d1286104676559ac26b08dcd2940a9775054566838319a2814acaae1d9a05fda8f4ed44773e8
|
7
|
+
data.tar.gz: cc3a71307b8d26498a31418de6742da0d33a6b0daec5dab0fe84696a5c5620ce92a89be7ab1823160b288dc71c41387decc07a7677e37e5ce92bf2f81e09afcd
|
data/.travis.yml
ADDED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
microfsm (0.0.
|
4
|
+
microfsm (0.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -17,7 +17,7 @@ GEM
|
|
17
17
|
rake (13.0.3)
|
18
18
|
regexp_parser (2.0.3)
|
19
19
|
rexml (3.2.4)
|
20
|
-
rubocop (1.
|
20
|
+
rubocop (1.9.1)
|
21
21
|
parallel (~> 1.10)
|
22
22
|
parser (>= 3.0.0.0)
|
23
23
|
rainbow (>= 2.2.2, < 4.0)
|
@@ -38,7 +38,7 @@ GEM
|
|
38
38
|
unicode-display_width (2.0.0)
|
39
39
|
|
40
40
|
PLATFORMS
|
41
|
-
|
41
|
+
x86_64-linux
|
42
42
|
|
43
43
|
DEPENDENCIES
|
44
44
|
bundler
|
@@ -50,4 +50,4 @@ DEPENDENCIES
|
|
50
50
|
simplecov
|
51
51
|
|
52
52
|
BUNDLED WITH
|
53
|
-
2.
|
53
|
+
2.2.6
|
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
|
1
|
+
MicroFSM
|
2
|
+
========
|
3
|
+
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/microfsm.svg)](https://badge.fury.io/rb/microfsm)
|
5
|
+
[![Build Status](https://travis-ci.org/matique/microfsm.svg?branch=master)](https://travis-ci.org/matique/microfsm)
|
2
6
|
|
3
7
|
MicroFSM implements a minimal/simple Finite-State Machine (FSM).
|
4
8
|
Transitions are triggered by events.
|
@@ -9,17 +13,15 @@ Finite-State Machine are described elsewhere, e.g. Wikipedia.
|
|
9
13
|
Several FSM implementations are available for Ruby.
|
10
14
|
Please feel free to use any of them if they fit better to your work.
|
11
15
|
|
12
|
-
The MicroFSM code consists of less than
|
16
|
+
The MicroFSM code consists of less than 100 locs.
|
13
17
|
No magic, no niceties, just an implementation using Ruby hashes.
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
## Installation
|
19
|
+
Installation
|
20
|
+
------------
|
19
21
|
|
20
22
|
~~~~
|
21
23
|
# Gemfile
|
22
|
-
gem "
|
24
|
+
gem "microfsm"
|
23
25
|
|
24
26
|
$ bundle install.
|
25
27
|
|
@@ -28,7 +30,8 @@ $ bundle install.
|
|
28
30
|
$ [sudo] gem install microfsm
|
29
31
|
~~~~
|
30
32
|
|
31
|
-
|
33
|
+
Usage
|
34
|
+
-----
|
32
35
|
|
33
36
|
~~~~
|
34
37
|
require 'microfsm'
|
@@ -53,8 +56,8 @@ fsm.trigger(:ignore) #=> true
|
|
53
56
|
fsm.state #=> :ignored
|
54
57
|
~~~~
|
55
58
|
|
56
|
-
You can also ask if an event will trigger a change in state.
|
57
|
-
the example above:
|
59
|
+
You can also ask if an event will trigger a change in state.
|
60
|
+
Following the example above:
|
58
61
|
|
59
62
|
~~~~
|
60
63
|
fsm.state #=> :ignored
|
@@ -66,6 +69,21 @@ fsm.trigger?(:reset) #=> true
|
|
66
69
|
fsm.state #=> :ignored
|
67
70
|
~~~~
|
68
71
|
|
72
|
+
Actions
|
73
|
+
-------
|
74
|
+
|
75
|
+
Adding actions to a transition is trivial:
|
76
|
+
|
77
|
+
~~~~
|
78
|
+
fsm.when(:confirm, new: :confirmed) { |event| count += 1 }
|
79
|
+
fsm.when(:confirm, new: :confirmed) { |event| foo(event) }
|
80
|
+
~~~~
|
81
|
+
|
82
|
+
Two actions/callbacks are triggered in the previous example.
|
83
|
+
|
84
|
+
|
85
|
+
Miscellaneous
|
86
|
+
-------------
|
69
87
|
|
70
88
|
Finally, you can list possible events or states:
|
71
89
|
|
@@ -80,9 +98,17 @@ fsm.triggerable_events #=> [:confirm, :ignore]
|
|
80
98
|
fsm.states #=> [:new, :confirmed, :ignored]
|
81
99
|
~~~~
|
82
100
|
|
101
|
+
And, the state can be set (which may be useful for testing purposes):
|
102
|
+
|
103
|
+
~~~~
|
104
|
+
fsm.state = :new
|
105
|
+
fsm.state #=> :new
|
106
|
+
~~~~
|
107
|
+
|
83
108
|
Check the examples directory for more information.
|
84
109
|
|
85
|
-
|
110
|
+
Links
|
111
|
+
-----
|
86
112
|
|
87
113
|
- [Wikipedia](https://en.wikipedia.org/wiki/Finite-state_machine)
|
88
114
|
- [micromachine](https://github.com/soveran/micromachine)
|
data/lib/microfsm.rb
CHANGED
@@ -34,11 +34,11 @@ class MicroFSM
|
|
34
34
|
|
35
35
|
def trigger!(event)
|
36
36
|
trigger(event) or
|
37
|
-
raise InvalidState.new(
|
37
|
+
raise InvalidState.new(msg(event))
|
38
38
|
end
|
39
39
|
|
40
40
|
def trigger?(event)
|
41
|
-
raise InvalidEvent unless @transitions_for.has_key?(event)
|
41
|
+
raise InvalidEvent.new(msg(event)) unless @transitions_for.has_key?(event)
|
42
42
|
|
43
43
|
@transitions_for[event].has_key?(state)
|
44
44
|
end
|
@@ -55,6 +55,10 @@ class MicroFSM
|
|
55
55
|
@transitions_for.values.map(&:to_a).flatten.uniq.sort
|
56
56
|
end
|
57
57
|
|
58
|
+
def state=(state)
|
59
|
+
@state = state
|
60
|
+
end
|
61
|
+
|
58
62
|
private
|
59
63
|
def transit(event)
|
60
64
|
callbacks = @callbacks_for[event][@state]
|
@@ -62,4 +66,8 @@ class MicroFSM
|
|
62
66
|
callbacks.each { |callback| callback.call(event) }
|
63
67
|
true
|
64
68
|
end
|
69
|
+
|
70
|
+
def msg(event)
|
71
|
+
"State: #{@state}; Event: #{event}"
|
72
|
+
end
|
65
73
|
end
|
data/lib/version.rb
CHANGED
data/microfsm.gemspec
CHANGED
@@ -5,10 +5,10 @@ Gem::Specification.new do |s|
|
|
5
5
|
s.name = "microfsm"
|
6
6
|
s.version = MicroFSM::VERSION
|
7
7
|
s.summary = %{Minimal Finite State Machine.}
|
8
|
-
|
8
|
+
s.description = %Q{MicroFSM implements a minimal/simple Finite-State Machine (FSM). Transitions are triggered by events. Actions for a transition can be added as callbacks.}
|
9
9
|
s.authors = ['Dittmar Krall']
|
10
10
|
s.email = 'dittmar.krall@matique.de'
|
11
|
-
s.homepage = 'http://www.matique.
|
11
|
+
s.homepage = 'http://www.matique.com'
|
12
12
|
s.license = "MIT"
|
13
13
|
|
14
14
|
s.files = `git ls-files`.split("\n")
|
data/test/microfsm_test.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: microfsm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dittmar Krall
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,8 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description:
|
55
|
+
description: MicroFSM implements a minimal/simple Finite-State Machine (FSM). Transitions
|
56
|
+
are triggered by events. Actions for a transition can be added as callbacks.
|
56
57
|
email: dittmar.krall@matique.de
|
57
58
|
executables: []
|
58
59
|
extensions: []
|
@@ -61,6 +62,7 @@ files:
|
|
61
62
|
- ".rubocop.yml"
|
62
63
|
- ".ruby-gemset"
|
63
64
|
- ".ruby-version"
|
65
|
+
- ".travis.yml"
|
64
66
|
- ".watchr"
|
65
67
|
- Gemfile
|
66
68
|
- Gemfile.lock
|
@@ -77,7 +79,7 @@ files:
|
|
77
79
|
- test/introspection_test.rb
|
78
80
|
- test/microfsm_test.rb
|
79
81
|
- test/test_helper.rb
|
80
|
-
homepage: http://www.matique.
|
82
|
+
homepage: http://www.matique.com
|
81
83
|
licenses:
|
82
84
|
- MIT
|
83
85
|
metadata: {}
|
@@ -96,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
98
|
- !ruby/object:Gem::Version
|
97
99
|
version: '0'
|
98
100
|
requirements: []
|
99
|
-
rubygems_version: 3.
|
101
|
+
rubygems_version: 3.2.6
|
100
102
|
signing_key:
|
101
103
|
specification_version: 4
|
102
104
|
summary: Minimal Finite State Machine.
|