pretty-fsm 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ Echoe.new('pretty-fsm') do |p|
8
8
  p.author = 'v01d'
9
9
  p.summary = "Finite State Machine class with intuitive integration"
10
10
  p.url = "http://github.com/v01d/pretty-fsm"
11
- p.version = "0.1"
11
+ p.version = "0.2"
12
12
  # p.dependencies = ['yard']
13
13
  # p.eval = proc { s.has_rdoc = 'yard' }
14
14
  end
@@ -11,7 +11,8 @@ module PrettyFSM
11
11
  @while_prefix,@start_prefix,@end_prefix = while_prefix,start_prefix,end_prefix
12
12
  @initial_state = initial_state
13
13
  @state = nil
14
- @transitions = Hash.new {|k,v| []}
14
+ @transitions = Hash.new {|h,k| h[k] = []}
15
+ @observers = []
15
16
  if (block_given?) then self.instance_eval(&Proc.new) end
16
17
  end
17
18
 
@@ -21,7 +22,7 @@ module PrettyFSM
21
22
  if (@state.nil?) then start(@initial_state) end
22
23
  call_fsm_method(@while_prefix)
23
24
 
24
- possible_next_state, = @transitions[@state].find {|state,condition| puts state,condition; @object.send(condition)}
25
+ possible_next_state, = @transitions[@state].find {|state,condition| @object.send(condition)}
25
26
  start(possible_next_state) unless possible_next_state.nil?
26
27
  end
27
28
 
@@ -29,6 +30,11 @@ module PrettyFSM
29
30
  def transition(options)
30
31
  @transitions[options[:from]].push([ options[:to], options[:if] ])
31
32
  end
33
+
34
+ # All "from" states defined so far with #transition
35
+ def defined_states
36
+ @transitions.keys
37
+ end
32
38
 
33
39
  # Transitions the FSM into the specified state, calling the 'end' and 'start' method, of the old and new states, respectively.
34
40
  # Any additional parameter passed to this method will be received by the corresponding 'start' method.
@@ -37,11 +43,24 @@ module PrettyFSM
37
43
  @state = new_state
38
44
  call_fsm_method(@start_prefix, args)
39
45
  end
46
+
47
+ # Adds +obj+ to the observer list. An observer receives all +while+, +start+ and +end+ (if defined) methods right after #object
48
+ def add_observer(obj)
49
+ @observers << obj
50
+ end
51
+
52
+ def remove_observer(obj)
53
+ @observers.delete(obj)
54
+ end
40
55
 
41
56
  private
42
57
  def call_fsm_method(prefix, args = [])
43
58
  method_name = (prefix + @state.to_s).to_sym
44
59
  if (@object.respond_to?(method_name)) then @object.send(method_name, *args) end
60
+
61
+ @observers.each do |observer|
62
+ if (observer.respond_to?(method_name)) then observer.send(method_name, *args) end
63
+ end
45
64
  end
46
65
  end
47
66
 
@@ -1,5 +1,5 @@
1
- file.reference.fsm-examples=examples
2
- file.reference.fsm-lib=lib
1
+ file.reference.pretty-fsm-examples=examples
2
+ file.reference.pretty-fsm-lib=lib
3
3
  javac.classpath=
4
4
  main.file=door.rb
5
5
  source.encoding=ISO-8859-1
@@ -3,7 +3,7 @@
3
3
  <type>org.netbeans.modules.ruby.rubyproject</type>
4
4
  <configuration>
5
5
  <data xmlns="http://www.netbeans.org/ns/ruby-project/1">
6
- <name>fsm</name>
6
+ <name>pretty-fsm</name>
7
7
  <source-roots>
8
8
  <root id="src.lib.dir"/>
9
9
  <root id="src.examples.dir"/>
data/pretty-fsm.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{pretty-fsm}
5
- s.version = "0.1"
5
+ s.version = "0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["v01d"]
9
- s.date = %q{2010-03-26}
9
+ s.date = %q{2010-07-02}
10
10
  s.description = %q{Finite State Machine class with intuitive integration}
11
11
  s.email = %q{}
12
12
  s.extra_rdoc_files = ["README", "lib/pretty-fsm.rb", "lib/pretty-fsm/fsm.rb", "lib/pretty-fsm/timed_event.rb"]
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- version: "0.1"
7
+ - 2
8
+ version: "0.2"
9
9
  platform: ruby
10
10
  authors:
11
11
  - v01d
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-03-26 00:00:00 -03:00
16
+ date: 2010-07-02 00:00:00 -03:00
17
17
  default_executable:
18
18
  dependencies: []
19
19