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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8380343e775bb2c143ccb457454ab729544226984831170d68d674faa5b674f
4
- data.tar.gz: 3b9cc48b46baac3f500309345312302ce79069b568043979efc220a1adf98ae2
3
+ metadata.gz: c13fcd2761ef9a4a28e56b5366bb9cde6a7e13a41014666fce63bd81b461445b
4
+ data.tar.gz: e95df3ce4f7f737d20a61b9ddb690eb5648eb3badfbd36ace291e2668db6a408
5
5
  SHA512:
6
- metadata.gz: 9ff4030bcdeb6a31ae300a1ec575aa252fb9f08267e8482eb361bd26884eb2b307bf30d397bfdd6e6a57bfb70f72900f593bf53f579e029dcbad5802d92ee561
7
- data.tar.gz: fe18e71eca3810d8e0a2b862fc37cb580854b11162116fd27c78933654cb46b93494ea95431dfd9bdfcedf0e0b0423c9340e58fbf1166c792a02c79792dc0028
6
+ metadata.gz: bca81a60f2f226f0e8b5e321d66a223df846e561d274465db6a5d1286104676559ac26b08dcd2940a9775054566838319a2814acaae1d9a05fda8f4ed44773e8
7
+ data.tar.gz: cc3a71307b8d26498a31418de6742da0d33a6b0daec5dab0fe84696a5c5620ce92a89be7ab1823160b288dc71c41387decc07a7677e37e5ce92bf2f81e09afcd
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+
3
+ bundle config set without production
4
+ script: "bundle exec rake test"
5
+
6
+ rvm:
7
+ - 2.7.2 # 2020-10-26
8
+
9
+ notifications:
10
+ email: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- microfsm (0.0.1)
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.8.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
- ruby
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.1.4
53
+ 2.2.6
data/README.md CHANGED
@@ -1,4 +1,8 @@
1
- # MicroFSM
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 70 locs.
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
- Check the examples directory for more information.
16
-
17
-
18
- ## Installation
19
+ Installation
20
+ ------------
19
21
 
20
22
  ~~~~
21
23
  # Gemfile
22
- gem "wysiwyg-rails"
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
- ## Usage
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. Following
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
- ## Links
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("Event '#{event}' not valid from state '#{@state}'")
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
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class MicroFSM
4
- VERSION = '0.0.1' # 2021-01-24
4
+ VERSION = '0.1.0' # 2021-02-08
5
+ # VERSION = '0.0.1' # 2021-01-24
5
6
  end
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
- # s.description = %Q{There are many finite state machine implementations for Ruby, and they all provide a nice DSL for declaring events, exceptions, callbacks, and all kinds of niceties in general.\n\nBut if all you want is a finite state machine, look no further: this has less than 50 lines of code and provides everything a finite state machine must have, and nothing more.}
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.de'
11
+ s.homepage = 'http://www.matique.com'
12
12
  s.license = "MIT"
13
13
 
14
14
  s.files = `git ls-files`.split("\n")
@@ -62,4 +62,11 @@ describe MicroFSM do
62
62
  fsm.when(:trigger, init: :wrong)
63
63
  end
64
64
  end
65
+
66
+ def test_state_assign
67
+ new_state = :unknown
68
+ refute_equal new_state, fsm.state
69
+ fsm.state = new_state
70
+ assert_equal new_state, fsm.state
71
+ end
65
72
  end
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.1
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-01-25 00:00:00.000000000 Z
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.de
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.1.4
101
+ rubygems_version: 3.2.6
100
102
  signing_key:
101
103
  specification_version: 4
102
104
  summary: Minimal Finite State Machine.