simplificator-fsm 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,32 @@
1
+ # fsm
2
+
3
+ FSM is a simple finite state machine
4
+
5
+ ## Usage
6
+ class Water
7
+ include FSM
8
+ define_fsm do
9
+ # now define all the states
10
+ # you can add :enter / :exit callbacks (callback can be a String, Symbol or Proc)
11
+ # these callbacks are triggered on any transition from/to this state
12
+
13
+ state(:gas)
14
+ state(:liquid)
15
+ state(:solid, :enter => :on_enter_solid, :exit => :on_exit_solid)
16
+
17
+ # define all valid transitions (name, from, to)
18
+ # you can define callbacks which are called only on this transition
19
+ transition(:heat, :solid, :liquid)
20
+ transition(:heat, :liquid, :gas) # look mam.... two transitions with same name
21
+ transition(:cooldown, :gas, :liquid)
22
+ transition(:cooldown, :liquid, :solid)
23
+
24
+ # define the attribute which is used to store the state (defaults to :state)
25
+ state(:state_of_material)
26
+
27
+ # define the initial state (defaults to the first state defined - :gas in this sample)
28
+ initial(:liquid)
29
+ end
30
+ end
31
+ ## Copyright
32
+ Copyright (c) 2009 simplificator GmbH. See LICENSE for details.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 0
4
+ :patch: 1
data/lib/fsm/machine.rb CHANGED
@@ -38,7 +38,7 @@ module FSM
38
38
  transition = Transition.new(name, from_state, to_state, options)
39
39
  from_state.add_transition(transition)
40
40
 
41
- define_transition_method(name, to_name)
41
+ define_transition_method(name)
42
42
 
43
43
  end
44
44
 
@@ -67,21 +67,35 @@ module FSM
67
67
 
68
68
  private
69
69
 
70
- def define_transition_method(name, to_name)
70
+ def define_transition_method(name)
71
71
  @target_class.instance_eval do
72
72
  define_method(name) do |*args|
73
73
  machine = Machine[self.class]
74
74
  from_name = Machine.get_current_state_name(self)
75
75
  from_state = machine.states[from_name]
76
- to_state = machine.states[to_name]
77
- transition = from_state.transitions[to_name]
78
- raise InvalidStateTransition.new("No transition defined from #{from_name} -> #{to_name}") unless transition
76
+
77
+ entry = from_state.transitions.detect() {|to_name, tr| tr.name == name}
78
+ transition = entry.last if entry
79
+ raise InvalidStateTransition.new("No transition with name '#{name}' defined from '#{from_name}'") unless transition
80
+ to_state = transition.to
79
81
 
80
82
  from_state.exit(self)
81
83
  transition.fire_event(self, args)
82
84
  to_state.enter(self)
83
- Machine.set_current_state_name(self, to_name)
84
- true # at the moment always return true ... as soon as we have guards or thelike this could be false as well
85
+ Machine.set_current_state_name(self, to_state.name)
86
+ true
87
+
88
+
89
+
90
+ #to_state = machine.states[to_name]
91
+ #transition = from_state.transitions[to_name]
92
+ #raise InvalidStateTransition.new("No transition defined from #{from_name} -> #{to_name}") unless transition
93
+
94
+ #from_state.exit(self)
95
+ #transition.fire_event(self, args)
96
+ #to_state.enter(self)
97
+ #Machine.set_current_state_name(self, to_name)
98
+ #true # at the moment always return true ... as soon as we have guards or thelike this could be false as well
85
99
  end
86
100
  end
87
101
  end
@@ -26,7 +26,7 @@ class Invoice
26
26
  end
27
27
  end
28
28
 
29
- class InvoiceTest < Test::Unit::TestCase
29
+ class InvoiceSampleTest < Test::Unit::TestCase
30
30
  context 'Invoice' do
31
31
 
32
32
  should 'Initial State is the first state defined unless no initial() call was made' do
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ class Water
4
+ attr_accessor(:state_of_material)
5
+ include FSM
6
+ define_fsm do
7
+ # now define all the states
8
+ # you can add :enter / :exit callbacks (callback can be a String, Symbol or Proc)
9
+ # these callbacks are triggered on any transition from/to this state and do not receive any arguments
10
+ state(:gas)
11
+ state(:liquid)
12
+ state(:solid, :enter => :on_enter_solid, :exit => :on_exit_solid)
13
+
14
+ # define all valid transitions (name, from, to). This will define a method with the given name.
15
+ # you can define :event callback which is called only on this transition and receives the arguments passed to the
16
+ # transition method
17
+ transition(:heat_up, :solid, :liquid)
18
+ transition(:heat_up, :liquid, :gas)
19
+ transition(:cool_down, :gas, :liquid)
20
+ transition(:cool_down, :liquid, :solid)
21
+
22
+ # define the attribute which is used to store the state (defaults to :state)
23
+ state_attribute(:state_of_material)
24
+
25
+ # define the initial state (defaults to the first state defined - :gas in this sample)
26
+ initial(:solid)
27
+ end
28
+ private
29
+ def on_enter_solid()
30
+ end
31
+ def on_exit_solid()
32
+ end
33
+
34
+ end
35
+
36
+ class WaterSampleTest < Test::Unit::TestCase
37
+ context 'Water' do
38
+
39
+ should 'cycle through material states' do
40
+ w = Water.new
41
+ assert_equal(:solid, w.state_of_material)
42
+ w.heat_up
43
+ assert_equal(:liquid, w.state_of_material)
44
+ w.heat_up
45
+ assert_equal(:gas, w.state_of_material)
46
+ w.cool_down
47
+ assert_equal(:liquid, w.state_of_material)
48
+ w.cool_down
49
+ assert_equal(:solid, w.state_of_material)
50
+
51
+ assert_raise(FSM::InvalidStateTransition) do
52
+ w.cool_down
53
+ end
54
+ end
55
+
56
+ end
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplificator-fsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - simplificator
@@ -21,10 +21,10 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
23
  - LICENSE
24
- - README.rdoc
24
+ - README.markdown
25
25
  files:
26
26
  - LICENSE
27
- - README.rdoc
27
+ - README.markdown
28
28
  - Rakefile
29
29
  - VERSION.yml
30
30
  - lib/fsm.rb
@@ -41,6 +41,7 @@ files:
41
41
  - test/state_test.rb
42
42
  - test/test_helper.rb
43
43
  - test/transition_test.rb
44
+ - test/water_sample_test.rb
44
45
  has_rdoc: true
45
46
  homepage: http://github.com/simplificator/fsm
46
47
  post_install_message:
@@ -74,3 +75,4 @@ test_files:
74
75
  - test/state_test.rb
75
76
  - test/test_helper.rb
76
77
  - test/transition_test.rb
78
+ - test/water_sample_test.rb
data/README.rdoc DELETED
@@ -1,7 +0,0 @@
1
- = fsm
2
-
3
- FSM is a simple finite state machine
4
-
5
- == Copyright
6
-
7
- Copyright (c) 2009 simplificator GmbH. See LICENSE for details.