soveran-micromachine 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +63 -0
- data/example/micromachine_sample.rb +7 -7
- data/example/micromachine_sample_gem.rb +7 -7
- data/lib/micromachine.rb +12 -12
- metadata +7 -12
- data/README.rdoc +0 -27
data/README.markdown
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
MicroMachine
|
2
|
+
============
|
3
|
+
|
4
|
+
Minimal Finite State Machine.
|
5
|
+
|
6
|
+
Description
|
7
|
+
-----------
|
8
|
+
|
9
|
+
There are many finite state machine implementations for Ruby, and they
|
10
|
+
all provide a nice DSL for declaring events, exceptions, callbacks,
|
11
|
+
and all kinds of niceties in general.
|
12
|
+
|
13
|
+
But if all you want is a finite state machine, look no further: this is only
|
14
|
+
15 lines of code and provides everything a finite state machine must have, and
|
15
|
+
nothing more.
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
|
20
|
+
require 'micromachine'
|
21
|
+
|
22
|
+
fsm = MicroMachine.new(:new) # Initial state.
|
23
|
+
|
24
|
+
fsm.transitions_for[:confirm] = { :new => :confirmed }
|
25
|
+
fsm.transitions_for[:ignore] = { :new => :ignored }
|
26
|
+
fsm.transitions_for[:reset] = { :confirmed => :new, :ignored => :new }
|
27
|
+
|
28
|
+
fsm.fire(:confirm) #=> true
|
29
|
+
fsm.fire(:ignore) #=> false
|
30
|
+
fsm.fire(:reset) #=> true
|
31
|
+
fsm.fire(:ignore) #=> true
|
32
|
+
|
33
|
+
Installation
|
34
|
+
------------
|
35
|
+
|
36
|
+
$ gem sources -a http://gems.github.com (you only have to do this once)
|
37
|
+
$ sudo gem install soveran-micromachine
|
38
|
+
|
39
|
+
License
|
40
|
+
-------
|
41
|
+
|
42
|
+
Copyright (c) 2009 Michel Martens for Citrusbyte
|
43
|
+
|
44
|
+
Permission is hereby granted, free of charge, to any person
|
45
|
+
obtaining a copy of this software and associated documentation
|
46
|
+
files (the "Software"), to deal in the Software without
|
47
|
+
restriction, including without limitation the rights to use,
|
48
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
49
|
+
copies of the Software, and to permit persons to whom the
|
50
|
+
Software is furnished to do so, subject to the following
|
51
|
+
conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be
|
54
|
+
included in all copies or substantial portions of the Software.
|
55
|
+
|
56
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
57
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
58
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
59
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
60
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
61
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
62
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
63
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,24 +1,24 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), '../lib/micromachine')
|
2
2
|
|
3
3
|
fsm = MicroMachine.new(:pending)
|
4
|
-
fsm.
|
5
|
-
fsm.
|
6
|
-
fsm.
|
4
|
+
fsm.transitions_for[:confirm] = { :pending => :confirmed }
|
5
|
+
fsm.transitions_for[:ignore] = { :pending => :ignored }
|
6
|
+
fsm.transitions_for[:reset] = { :confirmed => :pending, :ignored => :pending }
|
7
7
|
|
8
8
|
puts "Should print Confirmed, Reset and Ignored."
|
9
9
|
|
10
|
-
if fsm.
|
10
|
+
if fsm.trigger(:confirm)
|
11
11
|
puts "Confirmed"
|
12
12
|
end
|
13
13
|
|
14
|
-
if fsm.
|
14
|
+
if fsm.trigger(:ignore)
|
15
15
|
puts "Ignored"
|
16
16
|
end
|
17
17
|
|
18
|
-
if fsm.
|
18
|
+
if fsm.trigger(:reset)
|
19
19
|
puts "Reset"
|
20
20
|
end
|
21
21
|
|
22
|
-
if fsm.
|
22
|
+
if fsm.trigger(:ignore)
|
23
23
|
puts "Ignored"
|
24
24
|
end
|
@@ -2,24 +2,24 @@ require 'rubygems'
|
|
2
2
|
require 'micromachine'
|
3
3
|
|
4
4
|
fsm = MicroMachine.new(:pending)
|
5
|
-
fsm.
|
6
|
-
fsm.
|
7
|
-
fsm.
|
5
|
+
fsm.transitions_for[:confirm] = { :pending => :confirmed }
|
6
|
+
fsm.transitions_for[:ignore] = { :pending => :ignored }
|
7
|
+
fsm.transitions_for[:reset] = { :confirmed => :pending, :ignored => :pending }
|
8
8
|
|
9
9
|
puts "Should print Confirmed, Reset and Ignored."
|
10
10
|
|
11
|
-
if fsm.
|
11
|
+
if fsm.trigger(:confirm)
|
12
12
|
puts "Confirmed"
|
13
13
|
end
|
14
14
|
|
15
|
-
if fsm.
|
15
|
+
if fsm.trigger(:ignore)
|
16
16
|
puts "Ignored"
|
17
17
|
end
|
18
18
|
|
19
|
-
if fsm.
|
19
|
+
if fsm.trigger(:reset)
|
20
20
|
puts "Reset"
|
21
21
|
end
|
22
22
|
|
23
|
-
if fsm.
|
23
|
+
if fsm.trigger(:ignore)
|
24
24
|
puts "Ignored"
|
25
25
|
end
|
data/lib/micromachine.rb
CHANGED
@@ -4,27 +4,27 @@
|
|
4
4
|
#
|
5
5
|
# fsm = MicroMachine.new(:new) # Initial state.
|
6
6
|
#
|
7
|
-
# fsm.
|
8
|
-
# fsm.
|
9
|
-
# fsm.
|
7
|
+
# fsm.transitions_for[:confirm] = { :new => :confirmed }
|
8
|
+
# fsm.transitions_for[:ignore] = { :new => :ignored }
|
9
|
+
# fsm.transitions_for[:reset] = { :confirmed => :new, :ignored => :new }
|
10
10
|
#
|
11
|
-
# fsm.
|
12
|
-
# fsm.
|
13
|
-
# fsm.
|
14
|
-
# fsm.
|
11
|
+
# fsm.trigger(:confirm) #=> true
|
12
|
+
# fsm.trigger(:ignore) #=> false
|
13
|
+
# fsm.trigger(:reset) #=> true
|
14
|
+
# fsm.trigger(:ignore) #=> true
|
15
15
|
#
|
16
16
|
class MicroMachine
|
17
|
-
attr :
|
17
|
+
attr :transitions_for
|
18
18
|
attr :state
|
19
19
|
|
20
20
|
def initialize initial_state
|
21
21
|
@state = initial_state
|
22
|
-
@
|
22
|
+
@transitions_for = Hash.new
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
26
|
-
if
|
27
|
-
@state =
|
25
|
+
def trigger event
|
26
|
+
if transitions_for[event][@state]
|
27
|
+
@state = transitions_for[event][@state]
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soveran-micromachine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
@@ -19,25 +19,20 @@ executables: []
|
|
19
19
|
|
20
20
|
extensions: []
|
21
21
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
24
|
files:
|
25
25
|
- lib/micromachine.rb
|
26
|
-
- README.
|
26
|
+
- README.markdown
|
27
27
|
- LICENSE
|
28
28
|
- Rakefile
|
29
29
|
- example/micromachine_sample.rb
|
30
30
|
- example/micromachine_sample_gem.rb
|
31
|
-
has_rdoc:
|
31
|
+
has_rdoc: false
|
32
32
|
homepage: http://github.com/soveran/micromachine
|
33
33
|
post_install_message:
|
34
|
-
rdoc_options:
|
35
|
-
|
36
|
-
- --inline-source
|
37
|
-
- --title
|
38
|
-
- micromachine
|
39
|
-
- --main
|
40
|
-
- README.rdoc
|
34
|
+
rdoc_options: []
|
35
|
+
|
41
36
|
require_paths:
|
42
37
|
- lib
|
43
38
|
required_ruby_version: !ruby/object:Gem::Requirement
|
data/README.rdoc
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
= MicroMachine
|
2
|
-
|
3
|
-
Minimal Finite State Machine.
|
4
|
-
|
5
|
-
== Usage
|
6
|
-
|
7
|
-
|
8
|
-
require 'micromachine'
|
9
|
-
|
10
|
-
fsm = MicroMachine.new(:new) # Initial state.
|
11
|
-
|
12
|
-
fsm.events[:confirm] = { :new => :confirmed }
|
13
|
-
fsm.events[:ignore] = { :new => :ignored }
|
14
|
-
fsm.events[:reset] = { :confirmed => :new, :ignored => :new }
|
15
|
-
|
16
|
-
fsm.fire(:confirm) #=> true
|
17
|
-
fsm.fire(:ignore) #=> false
|
18
|
-
fsm.fire(:reset) #=> true
|
19
|
-
fsm.fire(:ignore) #=> true
|
20
|
-
|
21
|
-
== Installation
|
22
|
-
|
23
|
-
$ gem sources -a http://gems.github.com (you only have to do this once)
|
24
|
-
$ sudo gem install soveran-micromachine
|
25
|
-
|
26
|
-
Copyright (c) 2009 Michel Martens.
|
27
|
-
Released under the MIT license.
|