alexrevin-aasm_numerical 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/.document +5 -0
  2. data/.gitignore +11 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE +20 -0
  5. data/README.md +149 -0
  6. data/Rakefile +27 -0
  7. data/lib/alexrevin-aasm_numerical.rb +10 -0
  8. data/lib/alexrevin-aasm_numerical/aasm.rb +222 -0
  9. data/lib/alexrevin-aasm_numerical/event.rb +127 -0
  10. data/lib/alexrevin-aasm_numerical/localizer.rb +36 -0
  11. data/lib/alexrevin-aasm_numerical/persistence.rb +14 -0
  12. data/lib/alexrevin-aasm_numerical/persistence/active_record_persistence.rb +257 -0
  13. data/lib/alexrevin-aasm_numerical/state.rb +53 -0
  14. data/lib/alexrevin-aasm_numerical/state_machine.rb +31 -0
  15. data/lib/alexrevin-aasm_numerical/state_transition.rb +46 -0
  16. data/lib/alexrevin-aasm_numerical/supporting_classes.rb +6 -0
  17. data/lib/alexrevin-aasm_numerical/version.rb +3 -0
  18. data/spec/database.yml +3 -0
  19. data/spec/en.yml +10 -0
  20. data/spec/functional/conversation.rb +49 -0
  21. data/spec/functional/conversation_spec.rb +8 -0
  22. data/spec/schema.rb +7 -0
  23. data/spec/spec_helper.rb +16 -0
  24. data/spec/unit/aasm_spec.rb +462 -0
  25. data/spec/unit/active_record_persistence_spec.rb +246 -0
  26. data/spec/unit/before_after_callbacks_spec.rb +79 -0
  27. data/spec/unit/event_spec.rb +140 -0
  28. data/spec/unit/localizer_spec.rb +51 -0
  29. data/spec/unit/state_spec.rb +85 -0
  30. data/spec/unit/state_transition_spec.rb +163 -0
  31. data/test/functional/auth_machine_test.rb +148 -0
  32. data/test/models/process.rb +18 -0
  33. data/test/test_helper.rb +43 -0
  34. data/test/unit/aasm_test.rb +0 -0
  35. data/test/unit/event_test.rb +54 -0
  36. data/test/unit/state_machine_test.rb +37 -0
  37. data/test/unit/state_test.rb +69 -0
  38. data/test/unit/state_transition_test.rb +75 -0
  39. metadata +254 -0
@@ -0,0 +1,18 @@
1
+ module Models
2
+ class Process
3
+ include AASM
4
+
5
+ aasm_state :sleeping
6
+ aasm_state :running
7
+ aasm_state :suspended
8
+
9
+ aasm_event :start do
10
+ transitions :from => :sleeping, :to => :running
11
+ end
12
+
13
+ aasm_event :stop do
14
+ transitions :from => :running, :to => :suspended
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'ostruct'
12
+ require 'rubygems'
13
+
14
+ begin
15
+ gem 'minitest'
16
+ rescue Gem::LoadError
17
+ puts 'minitest gem not found'
18
+ end
19
+
20
+ begin
21
+ require 'minitest/autorun'
22
+ puts 'using minitest'
23
+ rescue LoadError
24
+ require 'test/unit'
25
+ puts 'using test/unit'
26
+ end
27
+
28
+ require 'rr'
29
+ require 'shoulda'
30
+
31
+ class Test::Unit::TestCase
32
+ include RR::Adapters::TestUnit
33
+ end
34
+
35
+ begin
36
+ require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = 'annoying'
37
+ require 'ruby-debug/completion'
38
+ rescue LoadError
39
+ end
40
+
41
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
42
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
43
+ require 'alexrevin-aasm_numerical'
File without changes
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class EventTest < Test::Unit::TestCase
4
+ def new_event
5
+ @event = AASM::SupportingClasses::Event.new(@name, {:success => @success}) do
6
+ transitions :to => :closed, :from => [:open, :received]
7
+ end
8
+ end
9
+
10
+ context 'event' do
11
+ setup do
12
+ @name = :close_order
13
+ @success = :success_callback
14
+ end
15
+
16
+ should 'set the name' do
17
+ assert_equal @name, new_event.name
18
+ end
19
+
20
+ should 'set the success option' do
21
+ assert_equal @success, new_event.success
22
+ end
23
+
24
+ should 'create StateTransitions' do
25
+ mock(AASM::SupportingClasses::StateTransition).new({:to => :closed, :from => :open})
26
+ mock(AASM::SupportingClasses::StateTransition).new({:to => :closed, :from => :received})
27
+ new_event
28
+ end
29
+
30
+ context 'when firing' do
31
+ should 'raise an AASM::InvalidTransition error if the transitions are empty' do
32
+ event = AASM::SupportingClasses::Event.new(:event)
33
+
34
+ obj = OpenStruct.new
35
+ obj.aasm_current_state = :open
36
+
37
+ assert_raise AASM::InvalidTransition do
38
+ event.fire(obj)
39
+ end
40
+ end
41
+
42
+ should 'return the state of the first matching transition it finds' do
43
+ event = AASM::SupportingClasses::Event.new(:event) do
44
+ transitions :to => :closed, :from => [:open, :received]
45
+ end
46
+
47
+ obj = OpenStruct.new
48
+ obj.aasm_current_state = :open
49
+
50
+ assert_equal :closed, event.fire(obj)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class StateMachineTest < Test::Unit::TestCase
4
+
5
+ context "state machines" do
6
+
7
+ should "be created without memory leak" do
8
+ assert_equal 1, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine
9
+ assert_number_of_objects AASM::SupportingClasses::State, 5 # AuthMachine
10
+ assert_number_of_objects AASM::SupportingClasses::Event, 11 # AuthMachine
11
+ assert_number_of_objects AASM::SupportingClasses::StateTransition, 19 # AuthMachine
12
+
13
+ load File.expand_path(File.dirname(__FILE__) + '/../models/process.rb')
14
+ assert_equal 2, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine + Process
15
+ assert_number_of_objects Models::Process, 0
16
+ assert_number_of_objects AASM::SupportingClasses::State, 8 # AuthMachine + Process
17
+ assert_number_of_objects AASM::SupportingClasses::Event, 13 # AuthMachine + Process
18
+ assert_number_of_objects AASM::SupportingClasses::StateTransition, 21 # AuthMachine + Process
19
+
20
+ Models.send(:remove_const, "Process") if Models.const_defined?("Process")
21
+ load File.expand_path(File.dirname(__FILE__) + '/../models/process.rb')
22
+ assert_equal 2, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine + Process
23
+ assert_number_of_objects AASM::SupportingClasses::State, 8 # AuthMachine + Process
24
+ assert_number_of_objects AASM::SupportingClasses::Event, 13 # AuthMachine + Process
25
+ assert_number_of_objects AASM::SupportingClasses::StateTransition, 21 # AuthMachine + Process
26
+ end
27
+
28
+ end
29
+
30
+ private
31
+
32
+ def assert_number_of_objects(clazz, num)
33
+ count = ObjectSpace.each_object(clazz) {}
34
+ assert_equal num, count, "#{num} expected, but we had #{count} #{clazz}"
35
+ end
36
+
37
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class StateTest < Test::Unit::TestCase
4
+ def new_state(options={})
5
+ AASM::SupportingClasses::State.new(@name, @options.merge(options))
6
+ end
7
+
8
+ context 'state' do
9
+ setup do
10
+ @name = :astate
11
+ @options = { :crazy_custom_key => 'key' }
12
+ end
13
+
14
+ should 'set the name' do
15
+ assert_equal :astate, new_state.name
16
+ end
17
+
18
+ should 'set the display_name from name' do
19
+ assert_equal "Astate", new_state.display_name
20
+ end
21
+
22
+ should 'set the display_name from options' do
23
+ assert_equal "A State", new_state(:display => "A State").display_name
24
+ end
25
+
26
+ should 'set the options and expose them as options' do
27
+ assert_equal @options, new_state.options
28
+ end
29
+
30
+ should 'equal a symbol of the same name' do
31
+ assert_equal new_state, :astate
32
+ end
33
+
34
+ should 'equal a state of the same name' do
35
+ assert_equal new_state, new_state
36
+ end
37
+
38
+ should 'send a message to the record for an action if the action is present as a symbol' do
39
+ state = new_state(:entering => :foo)
40
+ mock(record = Object.new).foo
41
+ state.call_action(:entering, record)
42
+ end
43
+
44
+ should 'send a message to the record for an action if the action is present as a string' do
45
+ state = new_state(:entering => 'foo')
46
+ mock(record = Object.new).foo
47
+ state.call_action(:entering, record)
48
+ end
49
+
50
+ should 'call a proc with the record as its argument for an action if the action is present as a proc' do
51
+ state = new_state(:entering => Proc.new {|r| r.foobar})
52
+ mock(record = Object.new).foobar
53
+ state.call_action(:entering, record)
54
+ end
55
+
56
+ should 'send a message to the record for each action if the action is present as an array' do
57
+ state = new_state(:entering => [:a, :b, 'c', lambda {|r| r.foobar}])
58
+
59
+ record = Object.new
60
+ mock(record).a
61
+ mock(record).b
62
+ mock(record).c
63
+ mock(record).foobar
64
+
65
+ state.call_action(:entering, record)
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ class StateTransitionTest < Test::Unit::TestCase
4
+ context 'state transition' do
5
+ setup do
6
+ @opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
7
+ @st = AASM::SupportingClasses::StateTransition.new(@opts)
8
+ end
9
+
10
+ should 'set from, to, and opts attr readers' do
11
+ assert_equal @opts[:from], @st.from
12
+ assert_equal @opts[:to], @st.to
13
+ assert_equal @opts, @st.options
14
+ end
15
+
16
+ should 'pass equality check if from and to are the same' do
17
+ obj = OpenStruct.new
18
+ obj.from = @opts[:from]
19
+ obj.to = @opts[:to]
20
+
21
+ assert_equal @st, obj
22
+ end
23
+
24
+ should 'fail equality check if from is not the same' do
25
+ obj = OpenStruct.new
26
+ obj.from = 'blah'
27
+ obj.to = @opts[:to]
28
+
29
+ assert_not_equal @st, obj
30
+ end
31
+
32
+ should 'fail equality check if to is not the same' do
33
+ obj = OpenStruct.new
34
+ obj.from = @opts[:from]
35
+ obj.to = 'blah'
36
+
37
+ assert_not_equal @st, obj
38
+ end
39
+
40
+ context 'when performing guard checks' do
41
+ should 'return true if there is no guard' do
42
+ opts = {:from => 'foo', :to => 'bar'}
43
+ st = AASM::SupportingClasses::StateTransition.new(opts)
44
+ assert st.perform(nil)
45
+ end
46
+
47
+ should 'call the method on the object if guard is a symbol' do
48
+ opts = {:from => 'foo', :to => 'bar', :guard => :test_guard}
49
+ st = AASM::SupportingClasses::StateTransition.new(opts)
50
+
51
+ mock(obj = Object.new).test_guard
52
+
53
+ st.perform(obj)
54
+ end
55
+
56
+ should 'call the method on the object if guard is a string' do
57
+ opts = {:from => 'foo', :to => 'bar', :guard => 'test_guard'}
58
+ st = AASM::SupportingClasses::StateTransition.new(opts)
59
+
60
+ mock(obj = Object.new).test_guard
61
+
62
+ st.perform(obj)
63
+ end
64
+
65
+ should 'call the proc passing the object if guard is a proc' do
66
+ opts = {:from => 'foo', :to => 'bar', :guard => Proc.new {|o| o.test_guard}}
67
+ st = AASM::SupportingClasses::StateTransition.new(opts)
68
+
69
+ mock(obj = Object.new).test_guard
70
+
71
+ st.perform(obj)
72
+ end
73
+ end
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,254 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alexrevin-aasm_numerical
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1
5
+ prerelease: false
6
+ segments:
7
+ - 2
8
+ - 3
9
+ - 1
10
+ version: 2.3.1
11
+ platform: ruby
12
+ authors:
13
+ - Scott Barron
14
+ - Scott Petersen
15
+ - Travis Tilley
16
+ - "Thorsten B\xC3\xB6ttger"
17
+ - Alex Revin
18
+ autorequire:
19
+ bindir: bin
20
+ cert_chain: []
21
+
22
+ date: 2011-09-13 00:00:00 +03:00
23
+ default_executable:
24
+ dependencies:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ prerelease: false
28
+ requirement: &id001 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ hash: 3
34
+ segments:
35
+ - 0
36
+ version: "0"
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: rake
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: sdoc
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rspec
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 2
78
+ - 0
79
+ version: "2.0"
80
+ type: :development
81
+ version_requirements: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ name: rr
84
+ prerelease: false
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ type: :development
95
+ version_requirements: *id005
96
+ - !ruby/object:Gem::Dependency
97
+ name: shoulda
98
+ prerelease: false
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ type: :development
109
+ version_requirements: *id006
110
+ - !ruby/object:Gem::Dependency
111
+ name: sqlite3
112
+ prerelease: false
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ type: :development
123
+ version_requirements: *id007
124
+ - !ruby/object:Gem::Dependency
125
+ name: minitest
126
+ prerelease: false
127
+ requirement: &id008 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ type: :development
137
+ version_requirements: *id008
138
+ - !ruby/object:Gem::Dependency
139
+ name: ruby-debug-completion
140
+ prerelease: false
141
+ requirement: &id009 !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ hash: 3
147
+ segments:
148
+ - 0
149
+ version: "0"
150
+ type: :development
151
+ version_requirements: *id009
152
+ description: AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects.
153
+ email: scott@elitists.net, ttilley@gmail.com alex@jaralex.ee
154
+ executables: []
155
+
156
+ extensions: []
157
+
158
+ extra_rdoc_files: []
159
+
160
+ files:
161
+ - .document
162
+ - .gitignore
163
+ - Gemfile
164
+ - LICENSE
165
+ - README.md
166
+ - Rakefile
167
+ - lib/alexrevin-aasm_numerical.rb
168
+ - lib/alexrevin-aasm_numerical/aasm.rb
169
+ - lib/alexrevin-aasm_numerical/event.rb
170
+ - lib/alexrevin-aasm_numerical/localizer.rb
171
+ - lib/alexrevin-aasm_numerical/persistence.rb
172
+ - lib/alexrevin-aasm_numerical/persistence/active_record_persistence.rb
173
+ - lib/alexrevin-aasm_numerical/state.rb
174
+ - lib/alexrevin-aasm_numerical/state_machine.rb
175
+ - lib/alexrevin-aasm_numerical/state_transition.rb
176
+ - lib/alexrevin-aasm_numerical/supporting_classes.rb
177
+ - lib/alexrevin-aasm_numerical/version.rb
178
+ - spec/database.yml
179
+ - spec/en.yml
180
+ - spec/functional/conversation.rb
181
+ - spec/functional/conversation_spec.rb
182
+ - spec/schema.rb
183
+ - spec/spec_helper.rb
184
+ - spec/unit/aasm_spec.rb
185
+ - spec/unit/active_record_persistence_spec.rb
186
+ - spec/unit/before_after_callbacks_spec.rb
187
+ - spec/unit/event_spec.rb
188
+ - spec/unit/localizer_spec.rb
189
+ - spec/unit/state_spec.rb
190
+ - spec/unit/state_transition_spec.rb
191
+ - test/functional/auth_machine_test.rb
192
+ - test/models/process.rb
193
+ - test/test_helper.rb
194
+ - test/unit/aasm_test.rb
195
+ - test/unit/event_test.rb
196
+ - test/unit/state_machine_test.rb
197
+ - test/unit/state_test.rb
198
+ - test/unit/state_transition_test.rb
199
+ has_rdoc: true
200
+ homepage: http://alexrevin.github.com/aasm_numerical/
201
+ licenses: []
202
+
203
+ post_install_message:
204
+ rdoc_options: []
205
+
206
+ require_paths:
207
+ - lib
208
+ required_ruby_version: !ruby/object:Gem::Requirement
209
+ none: false
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ hash: 3
214
+ segments:
215
+ - 0
216
+ version: "0"
217
+ required_rubygems_version: !ruby/object:Gem::Requirement
218
+ none: false
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ hash: 3
223
+ segments:
224
+ - 0
225
+ version: "0"
226
+ requirements: []
227
+
228
+ rubyforge_project:
229
+ rubygems_version: 1.3.7
230
+ signing_key:
231
+ specification_version: 3
232
+ summary: State machine mixin for Ruby objects
233
+ test_files:
234
+ - spec/database.yml
235
+ - spec/en.yml
236
+ - spec/functional/conversation.rb
237
+ - spec/functional/conversation_spec.rb
238
+ - spec/schema.rb
239
+ - spec/spec_helper.rb
240
+ - spec/unit/aasm_spec.rb
241
+ - spec/unit/active_record_persistence_spec.rb
242
+ - spec/unit/before_after_callbacks_spec.rb
243
+ - spec/unit/event_spec.rb
244
+ - spec/unit/localizer_spec.rb
245
+ - spec/unit/state_spec.rb
246
+ - spec/unit/state_transition_spec.rb
247
+ - test/functional/auth_machine_test.rb
248
+ - test/models/process.rb
249
+ - test/test_helper.rb
250
+ - test/unit/aasm_test.rb
251
+ - test/unit/event_test.rb
252
+ - test/unit/state_machine_test.rb
253
+ - test/unit/state_test.rb
254
+ - test/unit/state_transition_test.rb