ruby-state-machine 0.0.3 → 1.2.0

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.
@@ -1,14 +0,0 @@
1
- == 0.0.1 2009-08-11
2
-
3
- * 1 major enhancement:
4
- * Initial release
5
-
6
- == 0.0.2 2009-08-11
7
-
8
- * 1 minor enhancement:
9
- * Docs
10
-
11
- == 0.0.3 2009-08-17
12
-
13
- * 1 minor enhancement:
14
- * Change ruby-state-mach -> ruby-state-machine
@@ -1,18 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- PostInstall.txt
4
- README.rdoc
5
- Rakefile
6
- config/website.yml
7
- lib/ruby-state-machine.rb
8
- script/console
9
- script/destroy
10
- script/generate
11
- script/txt2html
12
- test/test_helper.rb
13
- test/test_state_machine.rb
14
- website/index.html
15
- website/index.txt
16
- website/javascripts/rounded_corners_lite.inc.js
17
- website/stylesheets/screen.css
18
- website/template.html.erb
@@ -1,7 +0,0 @@
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
-
@@ -1,48 +0,0 @@
1
- = ruby-state-machine
2
-
3
- * http://github.com/#{github_username}/#{project_name}
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 DELETED
@@ -1,26 +0,0 @@
1
- require 'rubygems'
2
- gem 'hoe', '>= 2.1.0'
3
- require 'hoe'
4
- require 'fileutils'
5
- require './lib/ruby-state-machine'
6
-
7
- Hoe.plugin :newgem
8
- Hoe.plugin :website
9
- # Hoe.plugin :cucumberfeatures
10
-
11
- # Generate all the Rake tasks
12
- # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
- $hoe = Hoe.spec 'ruby-state-machine' do
14
- self.developer 'stevenmiers', 'steven.miers@gmail.com'
15
- self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
- self.rubyforge_name = 'ruby-state-mach' #self.name # TODO this is default value
17
- # self.extra_deps = [['activesupport','>= 2.0.2']]
18
-
19
- end
20
-
21
- require 'newgem/tasks'
22
- Dir['tasks/**/*.rake'].each { |t| load t }
23
-
24
- # TODO - want other tests/tasks run by default? Add them to the list
25
- # remove_task :default
26
- # task :default => [:spec, :features]
@@ -1,2 +0,0 @@
1
- host: stevenmiers@rubyforge.org
2
- remote_dir: /var/www/gforge-projects/ruby-state-machine
@@ -1,10 +0,0 @@
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"
@@ -1,14 +0,0 @@
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)
@@ -1,14 +0,0 @@
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)
@@ -1,71 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- load File.dirname(__FILE__) + "/../Rakefile"
4
- require 'rubyforge'
5
- require 'redcloth'
6
- require 'syntax/convertors/html'
7
- require 'erb'
8
-
9
- download = "http://rubyforge.org/projects/#{$hoe.rubyforge_name}"
10
- version = $hoe.version
11
-
12
- def rubyforge_project_id
13
- RubyForge.new.configure.autoconfig["group_ids"][$hoe.rubyforge_name]
14
- end
15
-
16
- class Fixnum
17
- def ordinal
18
- # teens
19
- return 'th' if (10..19).include?(self % 100)
20
- # others
21
- case self % 10
22
- when 1: return 'st'
23
- when 2: return 'nd'
24
- when 3: return 'rd'
25
- else return 'th'
26
- end
27
- end
28
- end
29
-
30
- class Time
31
- def pretty
32
- return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
33
- end
34
- end
35
-
36
- def convert_syntax(syntax, source)
37
- return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
38
- end
39
-
40
- if ARGV.length >= 1
41
- src, template = ARGV
42
- template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
43
- else
44
- puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
45
- exit!
46
- end
47
-
48
- template = ERB.new(File.open(template).read)
49
-
50
- title = nil
51
- body = nil
52
- File.open(src) do |fsrc|
53
- title_text = fsrc.readline
54
- body_text_template = fsrc.read
55
- body_text = ERB.new(body_text_template).result(binding)
56
- syntax_items = []
57
- body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
58
- ident = syntax_items.length
59
- element, syntax, source = $1, $2, $3
60
- syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
61
- "syntax-temp-#{ident}"
62
- }
63
- title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
64
- body = RedCloth.new(body_text).to_html
65
- body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
66
- end
67
- stat = File.stat(src)
68
- created = stat.ctime
69
- modified = stat.mtime
70
-
71
- $stdout << template.result(binding)
@@ -1,3 +0,0 @@
1
- require 'stringio'
2
- require 'test/unit'
3
- require File.dirname(__FILE__) + '/../lib/ruby-state-machine'
@@ -1,212 +0,0 @@
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