soveran-micromachine 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Michel Martens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,31 @@
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
+ fsm.fire(:reset) do
22
+ puts "Executed only if the transition succeeds"
23
+ end
24
+
25
+ == Installation
26
+
27
+ $ gem sources -a http://gems.github.com (you only have to do this once)
28
+ $ sudo gem install soveran-micromachine
29
+
30
+ Copyright (c) 2009 Michel Martens.
31
+ Released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+ require 'rake/clean'
5
+
6
+ gem_spec_file = 'micromachine.gemspec'
7
+
8
+ gem_spec = eval(File.read(gem_spec_file)) rescue nil
9
+
10
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
11
+ pkg.need_zip = false
12
+ pkg.need_tar = false
13
+ rm_f FileList['pkg/**/*.*']
14
+ end if gem_spec
15
+
16
+ task :default => :gem
17
+
18
+ desc "Generate the gemspec file."
19
+ task :gemspec do
20
+ require 'erb'
21
+
22
+ File.open(gem_spec_file, 'w') do |f|
23
+ f.write ERB.new(File.read("#{gem_spec_file}.erb")).result(binding)
24
+ end
25
+ end
26
+
27
+ desc "Builds and installs the gem."
28
+ task :install => :repackage do
29
+ `sudo gem install pkg/#{gem_spec.name}-#{gem_spec.version}.gem`
30
+ end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), '../lib/micromachine')
2
+
3
+ fsm = MicroMachine.new(:pending)
4
+ fsm.events[:confirm] = { :pending => :confirmed }
5
+ fsm.events[:ignore] = { :pending => :ignored }
6
+ fsm.events[:reset] = { :confirmed => :pending, :ignored => :pending }
7
+
8
+ puts "Should print Confirmed, Reset and Ignored."
9
+
10
+ fsm.fire(:confirm) do
11
+ puts "Confirmed"
12
+ end
13
+
14
+ fsm.fire(:ignore) do
15
+ puts "Ignored"
16
+ end
17
+
18
+ fsm.fire(:reset) do
19
+ puts "Reset"
20
+ end
21
+
22
+ fsm.fire(:ignore) do
23
+ puts "Ignored"
24
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'micromachine'
3
+
4
+ fsm = MicroMachine.new(:pending)
5
+ fsm.events[:confirm] = { :pending => :confirmed }
6
+ fsm.events[:ignore] = { :pending => :ignored }
7
+ fsm.events[:reset] = { :confirmed => :pending, :ignored => :pending }
8
+
9
+ puts "Should print Confirmed, Reset and Ignored."
10
+
11
+ fsm.fire(:confirm) do
12
+ puts "Confirmed"
13
+ end
14
+
15
+ fsm.fire(:ignore) do
16
+ puts "Ignored"
17
+ end
18
+
19
+ fsm.fire(:reset) do
20
+ puts "Reset"
21
+ end
22
+
23
+ fsm.fire(:ignore) do
24
+ puts "Ignored"
25
+ end
@@ -0,0 +1,31 @@
1
+ # Finite State Machine
2
+ #
3
+ # Usage:
4
+ #
5
+ # fsm = MicroMachine.new(:new) # Initial state.
6
+ #
7
+ # fsm.events[:confirm] = { :new => :confirmed }
8
+ # fsm.events[:ignore] = { :new => :ignored }
9
+ # fsm.events[:reset] = { :confirmed => :new, :ignored => :new }
10
+ #
11
+ # fsm.fire(:confirm) #=> true
12
+ # fsm.fire(:ignore) #=> false
13
+ # fsm.fire(:reset) #=> true
14
+ # fsm.fire(:ignore) #=> true
15
+ #
16
+ class MicroMachine
17
+ attr :events
18
+ attr :state
19
+
20
+ def initialize initial_state
21
+ @state = initial_state
22
+ @events = Hash.new { |hash, key| hash[:key] = [] }
23
+ end
24
+
25
+ def fire event
26
+ if new_state = events[event][@state]
27
+ yield if block_given?
28
+ @state = new_state
29
+ end
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soveran-micromachine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-07 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: michel@soveran.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/micromachine.rb
26
+ - README.rdoc
27
+ - LICENSE
28
+ - Rakefile
29
+ - example/micromachine_sample.rb
30
+ - example/micromachine_sample_gem.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/soveran/micromachine
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --line-numbers
36
+ - --inline-source
37
+ - --title
38
+ - micromachine
39
+ - --main
40
+ - README.rdoc
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Minimal Finite State Machine.
62
+ test_files: []
63
+