ruby-state-mach 0.0.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/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-08-11
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/ruby-state-machine.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_helper.rb
11
+ test/test_state_machine.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on ruby-state-machine, see http://ruby-state-machine.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = ruby-state-machine
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/ruby-state-machine'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('ruby-state-mach', RubyStateMachine::VERSION) do |p|
7
+ p.developer('stevenmiers', 'steven.miers@gmail.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module RubyStateMachine
5
+ VERSION = '0.0.1'
6
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/ruby-state-machine.rb'}"
9
+ puts "Loading ruby-state-machine gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/ruby-state-machine'
@@ -0,0 +1,212 @@
1
+ require 'test/unit'
2
+ #require "#{File.dirname(__FILE__)}/../init"
3
+
4
+ require 'ruby-state-machine/state_machine'
5
+
6
+ # Fixture for testing state machine:
7
+ class StateMachineFixture
8
+ include StateMachine
9
+ attr_accessor :test_val, :prev_test_val, :proc_val, :force_decide_nil, :name_for_decider
10
+
11
+
12
+ state_machine :states => [:a_state, :b_state, :c_state, :d_state], :events => [:w_event, :x_event, :y_event, :z_event]
13
+ state_transition :state=>:a_state, :event=>:w_event, :next=>{:state=>:b_state, :action=>lambda{|o, args| o.increment_value(5)} }
14
+ state_transition :state=>:a_state, :event=>:x_event, :next=>:c_state
15
+ state_transition :state=>:a_state, :event=>:y_event, :next=>:a_state
16
+ state_transition :state=>:a_state, :event=>:z_event, :next=>:b_state
17
+
18
+ state_transition :state=>:b_state, :event=>:w_event, :next=>:b_state
19
+ state_transition :state=>:b_state, :event=>:y_event, :next=>:c_state
20
+ state_transition :state=>:b_state, :event=>:z_event, :next=>:a_state
21
+
22
+ state_transition :state=>:c_state, :event=>:x_event, :next=>:b_state
23
+ state_transition :state=>:c_state, :event=>:y_event, :next=>{:state=>:a_state, :action=>lambda{|o, args| o.increment_value(-3)}}
24
+
25
+ state_transition :state=>:c_state, :event=>:w_event, :decider => :c_state_decider,
26
+ :next=>[
27
+ {:state=>:a_state, :action=>lambda{|o, args| o.increment_value(7)} },
28
+ {:state=>:b_state, :action=>lambda{|o, args| o.increment_value(-10)} }
29
+ ]
30
+
31
+ state_transition :state=>:c_state, :event=>:z_event , :decider => :named_action_decider,
32
+ :next=>[
33
+ {:name=>"foo", :state=>:a_state, :action=>lambda{|o, args| o.increment_value(7)} },
34
+ {:name=>"bar", :state=>:d_state, :action=>lambda{|o, args| o.increment_value(-10)} }
35
+ ]
36
+
37
+ state_transition :state=>:d_state, :event=>:x_event, :next=>:stay
38
+
39
+ def initialize
40
+ super
41
+ @test_val = nil
42
+ @proc_val = 0
43
+ @force_decide_nil = false
44
+ @name_for_decider = "foo"
45
+ end
46
+
47
+ def increment_value(by)
48
+ @proc_val += by
49
+ end
50
+
51
+ def bush(term)
52
+ @prev_test_val = @test_val
53
+ @test_val=term
54
+ send_event(:w_event)
55
+ end
56
+
57
+ def c_state_decider(args)
58
+ if @force_decide_nil
59
+ # Hack to test nil decider:
60
+ nil
61
+ else
62
+ refined = (@test_val and @prev_test_val and @test_val.index(@prev_test_val)==0)
63
+ refined ? :b_state : :a_state
64
+ end
65
+ end
66
+
67
+ def named_action_decider(args)
68
+ @name_for_decider
69
+ end
70
+
71
+ end
72
+
73
+ # Test state machine:
74
+ class StateMachineTest < Test::Unit::TestCase
75
+
76
+ def test_states
77
+ assert_equal(:d_state, StateMachineFixture.states.last)
78
+ end
79
+
80
+ def test_events
81
+ assert_equal(:w_event, StateMachineFixture.events.first)
82
+ end
83
+
84
+ def test_next_states
85
+ assert_equal(:b_state, StateMachineFixture.next_state(:a_state, :w_event))
86
+ assert_equal(:c_state, StateMachineFixture.next_state(:a_state, :x_event))
87
+ assert_equal(:b_state, StateMachineFixture.next_state(:b_state, :w_event))
88
+ assert_equal(:a_state, StateMachineFixture.next_state(:b_state, :z_event))
89
+ assert_equal(:a_state, StateMachineFixture.next_state(:c_state, :y_event))
90
+ assert_equal(:b_state, StateMachineFixture.next_state(:c_state, :x_event))
91
+ assert_nil(StateMachineFixture.next_state(:b_state, :x_event))
92
+ end
93
+
94
+ def test_event_sending
95
+ sm = StateMachineFixture.new
96
+ assert_equal(:a_state, sm.current_state)
97
+ sm.send_event(:w_event)
98
+
99
+ assert_equal(:b_state, sm.current_state)
100
+ assert_raise(StateMachine::InvalidStateError){sm.send_event(:x_event)}
101
+
102
+ sm.send_event(:w_event)
103
+ assert_equal(:b_state, sm.current_state)
104
+
105
+ sm.send_event(:z_event)
106
+ assert_equal(:a_state, sm.current_state)
107
+
108
+ sm.send_event(:x_event)
109
+ assert_equal(:c_state, sm.current_state)
110
+ end
111
+
112
+ def test_action_calling
113
+ sm = StateMachineFixture.new
114
+ assert_equal(0, sm.proc_val)
115
+ assert_equal(:a_state, sm.current_state)
116
+ sm.send_event(:w_event)
117
+ assert_equal(5, sm.proc_val)
118
+
119
+ sm.send_event(:y_event)
120
+ assert_equal(5, sm.proc_val)
121
+
122
+ sm.send_event(:y_event)
123
+ assert_equal(2, sm.proc_val)
124
+ end
125
+
126
+ def test_decider
127
+ sm = StateMachineFixture.new
128
+ assert_equal(:a_state, sm.current_state)
129
+ sm.send_event(:x_event)
130
+ sm.bush("foo")
131
+ assert_equal(:a_state, sm.current_state)
132
+ sm.bush("foobar")
133
+ assert_equal(:b_state, sm.current_state)
134
+ sm.send_event(:y_event)
135
+ sm.bush("barfoo")
136
+ assert_equal(:a_state, sm.current_state)
137
+ end
138
+
139
+ def test_nil_decider
140
+ sm = StateMachineFixture.new
141
+ assert_equal(:a_state, sm.current_state)
142
+ sm.send_event(:x_event)
143
+ assert_equal(:c_state, sm.current_state)
144
+ sm.force_decide_nil=true
145
+ assert_raise(StateMachine::InvalidStateError){sm.send_event(:w_event)}
146
+ end
147
+
148
+ def test_multiple_actions_without_decider
149
+ assert_raise(ArgumentError) do
150
+ StateMachineFixture.state_transition :state=>:c_state, :event=>:z_event, #:decider => :c_state_decider,
151
+ :next=>[
152
+ {:state=>:a_state, :action=>lambda{|o, args| o.increment_value(7)} },
153
+ {:state=>:b_state, :action=>lambda{|o, args| o.increment_value(-10)} }
154
+ ]
155
+ end
156
+ end
157
+
158
+ def test_named_actions_in_decider
159
+ sm = StateMachineFixture.new
160
+ assert_equal(:a_state, sm.current_state)
161
+ sm.send_event(:x_event)
162
+ assert_equal(:c_state, sm.current_state)
163
+ sm.send_event(:z_event)
164
+
165
+ # Default value (foo) for named decider:
166
+ assert_equal(:a_state, sm.current_state)
167
+
168
+ # Back to a_state:
169
+ sm.send_event(:x_event)
170
+ assert_equal(:c_state, sm.current_state)
171
+
172
+ # Alt value (bar) for named decider:
173
+ sm.name_for_decider = 'bar'
174
+ sm.send_event(:z_event)
175
+ assert_equal(:d_state, sm.current_state)
176
+ end
177
+
178
+ def test_stay_state
179
+ sm = StateMachineFixture.new
180
+
181
+ # Get to :d state:
182
+ sm.send_event(:x_event)
183
+ sm.name_for_decider = 'bar'
184
+ sm.send_event(:z_event)
185
+ assert_equal(:d_state, sm.current_state)
186
+
187
+ # Send event & make sure we are still in :d state:
188
+ sm.send_event(:x_event)
189
+ assert_equal(:d_state, sm.current_state)
190
+ end
191
+
192
+ def test_event_history
193
+ sm = StateMachineFixture.new
194
+ assert_equal(0, sm.event_history.size)
195
+ sm.send_event(:x_event)
196
+ assert_equal(1, sm.event_history.size)
197
+ assert_equal([:x_event], sm.event_history)
198
+ sm.send_event(:y_event)
199
+ assert_equal(2, sm.event_history.size)
200
+ assert_equal([:x_event, :y_event], sm.event_history)
201
+ sm.send_event(:y_event)
202
+ assert_equal(3, sm.event_history.size)
203
+ assert_equal([:x_event, :y_event, :y_event], sm.event_history)
204
+
205
+ # Should max out at 10:
206
+ 15.times{sm.send_event(:z_event)}
207
+ assert_equal(10, sm.event_history.size)
208
+ assert_equal(Array.new(10, :z_event), sm.event_history)
209
+
210
+ end
211
+
212
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-state-mach
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - stevenmiers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-11 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.3.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ description: FIX (describe your package)
36
+ email:
37
+ - steven.miers@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - PostInstall.txt
46
+ - README.rdoc
47
+ files:
48
+ - History.txt
49
+ - Manifest.txt
50
+ - PostInstall.txt
51
+ - README.rdoc
52
+ - Rakefile
53
+ - lib/ruby-state-machine.rb
54
+ - script/console
55
+ - script/destroy
56
+ - script/generate
57
+ - test/test_helper.rb
58
+ - test/test_state_machine.rb
59
+ has_rdoc: true
60
+ homepage: FIX (url)
61
+ post_install_message: PostInstall.txt
62
+ rdoc_options:
63
+ - --main
64
+ - README.rdoc
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: ruby-state-mach
82
+ rubygems_version: 1.3.1
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: FIX (describe your package)
86
+ test_files:
87
+ - test/test_helper.rb
88
+ - test/test_state_machine.rb