statemachine 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,5 +1,15 @@
1
1
  = StateMachine Changelog
2
2
 
3
+ == Version 0.0.3
4
+
5
+ Bug fix dealing with entry and exit actions. The state machine's state need to be set to the entered/exited state before calling the
6
+ exit/entry action.
7
+ * added a couple specs in the exit_entry_spec
8
+ * modified state.entered/exited methods to set the state
9
+ * modifed the StateMachine.state to accept state objects.
10
+ * removed running attribute from StateMachine because it wasn't much use
11
+ * also removed the nil (end state)
12
+
3
13
  == Version 0.0.2
4
14
 
5
15
  More conventional file structure
@@ -41,15 +41,21 @@ module StateMachine
41
41
 
42
42
  def exit(args)
43
43
  @statemachine.trace("\texiting #{self}")
44
+ activate
44
45
  call_proc(@exit_action, args, "exit action for #{self}") if @exit_action
45
46
  @superstate.existing(self) if @superstate
46
47
  end
47
48
 
48
49
  def enter(args)
49
50
  @statemachine.trace("\tentering #{self}")
51
+ activate
50
52
  call_proc(@entry_action, args, "entry action for #{self}") if @entry_action
51
53
  end
52
54
 
55
+ def activate
56
+ @statemachine.state = self
57
+ end
58
+
53
59
  def is_superstate?
54
60
  return false
55
61
  end
@@ -15,7 +15,7 @@ module StateMachine
15
15
 
16
16
  include ProcCalling
17
17
 
18
- attr_reader :states, :state, :running
18
+ attr_reader :states, :state
19
19
  attr_accessor :start_state, :tracer
20
20
 
21
21
  def initialize
@@ -34,7 +34,6 @@ module StateMachine
34
34
 
35
35
  def run
36
36
  @state = @start_state
37
- @running = true
38
37
  end
39
38
  alias :reset :run
40
39
 
@@ -43,7 +42,9 @@ module StateMachine
43
42
  end
44
43
 
45
44
  def state= value
46
- if @states[value]
45
+ if value.is_a? State
46
+ @state = value
47
+ elsif @states[value]
47
48
  @state = @states[value]
48
49
  elsif value and @states[value.to_sym]
49
50
  @state = @states[value.to_sym]
@@ -55,11 +56,10 @@ module StateMachine
55
56
  if @state
56
57
  transition = @state.transitions[event]
57
58
  if transition
58
- @state = transition.invoke(@state, args)
59
+ transition.invoke(@state, args)
59
60
  else
60
61
  raise MissingTransitionException.new("#{@state} does not respond to the '#{event}' event.")
61
62
  end
62
- @running = @state != nil
63
63
  else
64
64
  raise StateMachineException.new("The state machine isn't in any state. Did you forget to call run?")
65
65
  end
@@ -29,8 +29,6 @@ module StateMachine
29
29
  terminal_state = terminal_state.start_state
30
30
  terminal_state.enter(args)
31
31
  end
32
-
33
- return terminal_state
34
32
  end
35
33
 
36
34
  def exits_and_entries(origin)
@@ -3,7 +3,7 @@ module StateMachine
3
3
  unless defined? MAJOR
4
4
  MAJOR = 0
5
5
  MINOR = 0
6
- TINY = 2
6
+ TINY = 3
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  TAG = "REL_" + [MAJOR, MINOR, TINY].join('_')
@@ -44,6 +44,35 @@ context "State Machine Entry and Exit Actions" do
44
44
  @log.join(",").should_equal "exited_off(one),on,entered_on(one,two)"
45
45
  end
46
46
 
47
+ specify "current state is set prior to exit and entry actions" do
48
+ @sm[:off].on_exit Proc.new { @log << @sm.state.id }
49
+ @sm[:on].on_entry Proc.new { @log << @sm.state.id }
50
+
51
+ @sm.toggle
52
+
53
+ @log.join(",").should_equal "off,on,on"
54
+ end
55
+
56
+ specify "current state is set prior to exit and entry actions even with super states" do
57
+ @sm.add(:off_super, :toggle, :on, Proc.new { @log << "super_on" } )
58
+ @sm.add(:on_super, :toggle, :off, Proc.new { @log << "super_off" } )
59
+ @sm[:off_super].add_substates(:off)
60
+ @sm[:on_super].add_substates(:on)
61
+ @sm[:off_super].on_exit Proc.new { @log << @sm.state.id }
62
+ @sm[:on_super].on_entry Proc.new { @log << @sm.state.id }
63
+
64
+ @sm.toggle
65
+ @log.join(",").should_equal "off_super,super_on,on_super"
66
+ end
67
+
68
+ specify "entry actions invokes another event" do
69
+ @sm[:on].on_entry Proc.new { @sm.toggle }
70
+
71
+ @sm.toggle
72
+ @log.join(",").should_equal "on,off"
73
+ @sm.state.id.should_be :off
74
+ end
75
+
47
76
 
48
77
 
49
78
  end
@@ -25,16 +25,6 @@ context "simple cases:" do
25
25
  transition = origin[:flip]
26
26
  check_transition(transition, :on, :off, :flip, @proc)
27
27
  end
28
-
29
- specify "end state" do
30
- @sm.add(:start, :blah, nil, @proc)
31
-
32
- @sm.run
33
- @sm.running.should.be true
34
- @sm.process_event(:blah)
35
- @sm.state.should.be nil
36
- @sm.running.should.be false;
37
- end
38
28
 
39
29
  specify "reset" do
40
30
  @sm.add(:start, :blah, nil, @proc)
@@ -44,7 +34,6 @@ context "simple cases:" do
44
34
  @sm.reset
45
35
 
46
36
  @sm.state.should.be @sm[:start]
47
- @sm.running.should.be true
48
37
  end
49
38
 
50
39
  specify "exception when state machine is not running" do
metadata CHANGED
@@ -3,9 +3,9 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: statemachine
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2006-10-25 00:00:00 -05:00
8
- summary: StateMachine-0.0.2 - State Machine Library for Ruby http://statemachine.rubyforge.org/
6
+ version: 0.0.3
7
+ date: 2006-10-26 00:00:00 -05:00
8
+ summary: StateMachine-0.0.3 - State Machine Library for Ruby http://statemachine.rubyforge.org/
9
9
  require_paths:
10
10
  - lib
11
11
  email: statemachine-devel@rubyforge.org