barebone-fsm 0.0.2 → 0.0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013, Md. Imrul Hassan
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc CHANGED
@@ -1,9 +1,9 @@
1
1
  == Overview
2
- This module implements a basic finite-state machine (FSM).
2
+ This module, barebone-fsm, implements a basic finite-state machine (FSM).
3
3
  An FSM consists of a finite number of states,
4
4
  with one of them being the current state of the FSM.
5
5
  Transitions are defined between states, which are triggered on events.
6
- For details on FSM, see this wiki page: {FSM}[http://en.wikipedia.org/wiki/Finite-state_machine].
6
+ For details on FSM, see this wiki page: {http://en.wikipedia.org/wiki/Finite-state_machine FSM}.
7
7
 
8
8
  The motivation behind the module was to implement a very basic barebone FSM in Ruby.
9
9
  Features are kept at minimum as well as the code.
@@ -12,7 +12,8 @@ Only two classes for the FSM are defined as the following:
12
12
  * FSMState -> the state class
13
13
 
14
14
  Author:: Md. Imrul Hassan (mailto:mihassan@gmail.com)
15
- Copyright:: Copyright: Md. Imrul Hassan, 2013
15
+ Copyright:: Copyright (c) 2013, Md. Imrul Hassan
16
+ License:: Barebone-fsm is released under the MIT license. Please check the {file:MIT-LICENSE LICENSE} file for more details.
16
17
 
17
18
  == Features
18
19
  Apart from having support for states and events, this module offers the following features:
data/example/door.rb ADDED
@@ -0,0 +1,51 @@
1
+ require '../lib/barebone-fsm'
2
+
3
+ fsm = FSM::FSM.new(:default)
4
+
5
+ fsm.build do
6
+
7
+ state :default do
8
+ event :open do
9
+ puts "#{@x} transition: default->open"
10
+ :open
11
+ end
12
+ event :close do
13
+ puts "#{@x} transition: default->close"
14
+ :close
15
+ end
16
+ end
17
+
18
+ state :open do
19
+ event :close do
20
+ @x ||= 0
21
+ @x+=1
22
+ puts "#{@x} transition: open->close"
23
+ :close
24
+ end
25
+ end
26
+
27
+ state :close do
28
+ event :open do
29
+ @x ||= 0
30
+ @x+=1
31
+ puts "#{@x} transition: close->open"
32
+ :open
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ puts fsm
39
+
40
+ fsm.run do
41
+
42
+ event :close
43
+ event :open
44
+ event :close
45
+ event :undefined
46
+ event :open
47
+ event :close
48
+
49
+ end
50
+
51
+ puts fsm
@@ -0,0 +1,49 @@
1
+ require '../lib/barebone-fsm'
2
+
3
+ fsm = FSM::FSM.new
4
+
5
+ fsm.build do
6
+
7
+ state :stopped do
8
+ event :open do
9
+ puts "[#{@state}]->#{@event}"
10
+ :open
11
+ end
12
+ event :start do
13
+ puts "[#{@state}]->#{@event}"
14
+ :started
15
+ end
16
+ end
17
+
18
+ state :open do
19
+ event :close do
20
+ puts "[#{@state}]->#{@event}"
21
+ :stopped
22
+ end
23
+ end
24
+
25
+ state :started do
26
+ event :open do
27
+ puts "[#{@state}]->#{@event}"
28
+ :open
29
+ end
30
+ event :stop do
31
+ puts "[#{@state}]->#{@event}"
32
+ :stopped
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ fsm.run do
39
+
40
+ event :open
41
+ event :close
42
+ event :start
43
+ event :open
44
+ event :close
45
+ event :start
46
+ event :stop
47
+ end
48
+
49
+ puts fsm
data/lib/barebone-fsm.rb CHANGED
@@ -91,12 +91,15 @@ module FSM
91
91
  # end
92
92
  #
93
93
  class FSM
94
+
94
95
  attr_writer :event
96
+
97
+ # Creates a new FSM object with an optional default state set.
95
98
  def initialize(default_state=nil)
96
99
  @states = {}
97
100
  if default_state then
98
- @state = @default = default_state
99
- @states[@state] = FSMState.new(self, @state)
101
+ @default = default_state
102
+ @states[@default] = FSMState.new(self, @default)
100
103
  end
101
104
  end
102
105
 
@@ -0,0 +1,58 @@
1
+ require '../lib/barebone-fsm'
2
+
3
+ describe FSM::FSM do
4
+
5
+ context "with default state" do
6
+
7
+ before :each do
8
+ @fsm = FSM::FSM.new :default
9
+ end
10
+
11
+ it "should set the default state" do
12
+ @fsm.build do
13
+ state :start do
14
+ end
15
+ end
16
+ @fsm[].state.should be_eql(:start)
17
+ @fsm.run do
18
+ event :undefined
19
+ end
20
+ @fsm[].state.should be_eql(:default)
21
+ end
22
+
23
+ end
24
+
25
+ context "without default state" do
26
+
27
+ before :each do
28
+ @fsm = FSM::FSM.new
29
+ end
30
+
31
+ it "should not set the default state" do
32
+ @fsm.build do
33
+ state :start do
34
+ end
35
+ end
36
+ @fsm[].state.should be_eql(:start)
37
+ @fsm.run do
38
+ event :undefined
39
+ end
40
+ @fsm[].state.should be_eql(:start)
41
+ end
42
+
43
+ end
44
+
45
+ it "should create states on the fly" do
46
+ @fsm = FSM::FSM.new
47
+ @fsm[:new_state].state.should eq(:new_state)
48
+ @fsm.state(:another_state).state.should eq(:another_state)
49
+ end
50
+
51
+ it "should set current state to the first accessed state" do
52
+ @fsm = FSM::FSM.new
53
+ @fsm[:new_state].state.should eq(:new_state)
54
+ @fsm.state(:another_state).state.should eq(:another_state)
55
+ @fsm.state().state.should eq(:new_state)
56
+ end
57
+
58
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barebone-fsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-01-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: A barebone implementation of finite state machine keeping simplicity
14
+ description: A barebone implementation of the finite state machine keeping simplicity
15
15
  in mind.
16
16
  email: mihassan@gmail.com
17
17
  executables: []
@@ -19,10 +19,15 @@ extensions: []
19
19
  extra_rdoc_files:
20
20
  - README.rdoc
21
21
  files:
22
+ - example/door.rb
23
+ - example/microwave.rb
24
+ - spec/barebone-fsm_spec.rb
22
25
  - lib/barebone-fsm.rb
23
26
  - README.rdoc
24
- homepage: http://rubygems.org/gems/barebone-fsm
25
- licenses: []
27
+ - MIT-LICENSE
28
+ homepage: https://github.com/mihassan/barebone-fsm/
29
+ licenses:
30
+ - MIT
26
31
  post_install_message:
27
32
  rdoc_options: []
28
33
  require_paths: