state_manager 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,171 @@
1
+ require 'helper'
2
+
3
+ class TransitionsTest < Test::Unit::TestCase
4
+
5
+ class UserStates < StateManager::Base
6
+ module TrackEnterExitCounts
7
+ attr_reader :enter_count
8
+ attr_reader :exit_count
9
+
10
+ def initialize(*args)
11
+ super(*args)
12
+ @enter_count = 0
13
+ @exit_count = 0
14
+ end
15
+
16
+ def enter
17
+ @enter_count += 1
18
+ end
19
+
20
+ def exit
21
+ @exit_count += 1
22
+ end
23
+ end
24
+
25
+ attr_reader :enter_counts, :exit_counts
26
+
27
+ def initialize(target, options={})
28
+ super(target, options)
29
+ @enter_counts = {}
30
+ @exit_counts = {}
31
+ end
32
+
33
+ initial_state :unregistered
34
+ event :ban, :transitions_to => 'inactive.banned' do |reason=nil|
35
+ user.notes = "Banned because: #{reason}"
36
+ end
37
+ state :unregistered do
38
+ include TrackEnterExitCounts
39
+
40
+ event :register do
41
+ if resource.paid
42
+ transition_to 'active.premium'
43
+ else
44
+ transition_to 'active.default'
45
+ end
46
+ end
47
+ end
48
+ state :active do
49
+ event :ping
50
+ state :default do
51
+ end
52
+ state :premium do
53
+ event :ping do
54
+ resource.has_prizes = true
55
+ end
56
+ end
57
+ end
58
+ state :inactive do
59
+ include TrackEnterExitCounts
60
+
61
+ state :banned do
62
+ include TrackEnterExitCounts
63
+
64
+ event :appeal, :transitions_to => :appealing
65
+ end
66
+
67
+ state :appealing do
68
+ include TrackEnterExitCounts
69
+ end
70
+
71
+ event :unban, :transitions_to => 'active.default'
72
+ end
73
+
74
+ attr_accessor :will_current_state, :will_from, :will_to, :will_event
75
+ def will_transition(from, to, event)
76
+ self.will_current_state = current_state
77
+ self.will_from = from
78
+ self.will_to = to
79
+ self.will_event = event
80
+ end
81
+
82
+ attr_accessor :did_current_state, :did_from, :did_to, :did_event
83
+ def did_transition(from, to, event)
84
+ self.did_current_state = current_state
85
+ self.did_from = from
86
+ self.did_to = to
87
+ self.did_event = event
88
+ end
89
+
90
+ end
91
+
92
+ class User
93
+ attr_accessor :state, :notes, :paid, :has_prizes
94
+ extend StateManager::Resource
95
+ state_manager
96
+ end
97
+
98
+ def setup
99
+ @resource = User.new
100
+ end
101
+
102
+ def test_custom_transition
103
+ @resource.paid = true
104
+ @resource.register!
105
+
106
+ assert_equal 'register', @resource.state_manager.did_event.to_s
107
+ assert @resource.active_premium?
108
+
109
+ @resource.state_manager.transition_to 'unregistered'
110
+ @resource.paid = false
111
+ @resource.register!
112
+
113
+ assert_equal 'register', @resource.state_manager.did_event.to_s
114
+ assert @resource.active_default?
115
+ end
116
+
117
+ def test_enter_exit
118
+ assert_equal 1, @resource.state_manager.find_state('unregistered').enter_count
119
+ assert_equal 0, @resource.state_manager.find_state('unregistered').exit_count
120
+
121
+ @resource.state_manager.transition_to('inactive.banned')
122
+
123
+ assert_equal 1, @resource.state_manager.find_state('inactive').enter_count
124
+ assert_equal 0, @resource.state_manager.find_state('inactive').exit_count
125
+ assert_equal 1, @resource.state_manager.find_state('inactive.banned').enter_count
126
+ assert_equal 0, @resource.state_manager.find_state('inactive.banned').exit_count
127
+
128
+ @resource.state_manager.transition_to(:unregistered)
129
+
130
+ assert_equal 1, @resource.state_manager.find_state('inactive').enter_count
131
+ assert_equal 1, @resource.state_manager.find_state('inactive').exit_count
132
+ assert_equal 1, @resource.state_manager.find_state('inactive.banned').enter_count
133
+ assert_equal 1, @resource.state_manager.find_state('inactive.banned').exit_count
134
+
135
+ @resource.state_manager.transition_to('inactive.banned')
136
+
137
+ assert_equal 2, @resource.state_manager.find_state('inactive').enter_count
138
+ assert_equal 1, @resource.state_manager.find_state('inactive').exit_count
139
+ assert_equal 2, @resource.state_manager.find_state('inactive.banned').enter_count
140
+ assert_equal 1, @resource.state_manager.find_state('inactive.banned').exit_count
141
+
142
+ @resource.state_manager.transition_to('inactive.appealing')
143
+
144
+ assert_equal 2, @resource.state_manager.find_state('inactive').enter_count
145
+ assert_equal 1, @resource.state_manager.find_state('inactive').exit_count
146
+ assert_equal 2, @resource.state_manager.find_state('inactive.banned').enter_count
147
+ assert_equal 2, @resource.state_manager.find_state('inactive.banned').exit_count
148
+ assert_equal 1, @resource.state_manager.find_state('inactive.appealing').enter_count
149
+ assert_equal 0, @resource.state_manager.find_state('inactive.appealing').exit_count
150
+ end
151
+
152
+ def test_event_with_args
153
+ @resource.ban!('brogrammer')
154
+ assert_equal 'Banned because: brogrammer', @resource.notes
155
+ end
156
+
157
+ def test_will_and_did_transition
158
+ @resource.ban!
159
+
160
+ assert_equal 'unregistered', @resource.state_manager.will_current_state.to_s
161
+ assert_equal 'unregistered', @resource.state_manager.will_from.to_s
162
+ assert_equal 'inactive.banned', @resource.state_manager.will_to.to_s
163
+ assert_equal :ban, @resource.state_manager.will_event
164
+
165
+ assert_equal 'inactive.banned', @resource.state_manager.did_current_state.to_s
166
+ assert_equal 'unregistered', @resource.state_manager.did_from.to_s
167
+ assert_equal 'inactive.banned', @resource.state_manager.did_to.to_s
168
+ assert_equal :ban, @resource.state_manager.did_event
169
+ end
170
+
171
+ end
metadata ADDED
@@ -0,0 +1,226 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state_manager
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.3
6
+ platform: ruby
7
+ authors:
8
+ - Gordon Hempton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-06-06 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rdoc
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: "3.12"
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: pry
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: pry-doc
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: pry-remote
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry-nav
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: pry-stack_explorer
83
+ requirement: &id007 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *id007
92
+ - !ruby/object:Gem::Dependency
93
+ name: bundler
94
+ requirement: &id008 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: 1.0.0
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *id008
103
+ - !ruby/object:Gem::Dependency
104
+ name: jeweler
105
+ requirement: &id009 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 1.8.3
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: *id009
114
+ - !ruby/object:Gem::Dependency
115
+ name: delayed_job_active_record
116
+ requirement: &id010 !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: "0"
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: *id010
125
+ - !ruby/object:Gem::Dependency
126
+ name: activerecord
127
+ requirement: &id011 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: "0"
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: *id011
136
+ - !ruby/object:Gem::Dependency
137
+ name: sqlite3
138
+ requirement: &id012 !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: "0"
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: *id012
147
+ - !ruby/object:Gem::Dependency
148
+ name: timecop
149
+ requirement: &id013 !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: "0"
155
+ type: :development
156
+ prerelease: false
157
+ version_requirements: *id013
158
+ description: Finite state machine implementation that keeps logic separate from model classes and supports sub-states.
159
+ email: ghempton@gmail.com
160
+ executables: []
161
+
162
+ extensions: []
163
+
164
+ extra_rdoc_files:
165
+ - LICENSE.txt
166
+ - README.md
167
+ files:
168
+ - .document
169
+ - Gemfile
170
+ - Gemfile.lock
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - VERSION
175
+ - lib/state_manager.rb
176
+ - lib/state_manager/adapters.rb
177
+ - lib/state_manager/adapters/active_record.rb
178
+ - lib/state_manager/adapters/base.rb
179
+ - lib/state_manager/base.rb
180
+ - lib/state_manager/core.rb
181
+ - lib/state_manager/dsl.rb
182
+ - lib/state_manager/helpers.rb
183
+ - lib/state_manager/plugins.rb
184
+ - lib/state_manager/plugins/delayed_job.rb
185
+ - lib/state_manager/resource.rb
186
+ - lib/state_manager/state.rb
187
+ - state_manager.gemspec
188
+ - test/adapters/active_record_test.rb
189
+ - test/basic_test.rb
190
+ - test/definition_test.rb
191
+ - test/helper.rb
192
+ - test/helpers_test.rb
193
+ - test/plugins/delayed_job_test.rb
194
+ - test/transitions_test.rb
195
+ homepage: http://github.com/ghempton/statemanager
196
+ licenses:
197
+ - MIT
198
+ post_install_message:
199
+ rdoc_options: []
200
+
201
+ require_paths:
202
+ - lib
203
+ required_ruby_version: !ruby/object:Gem::Requirement
204
+ none: false
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ hash: -4414666948627787834
209
+ segments:
210
+ - 0
211
+ version: "0"
212
+ required_rubygems_version: !ruby/object:Gem::Requirement
213
+ none: false
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: "0"
218
+ requirements: []
219
+
220
+ rubyforge_project:
221
+ rubygems_version: 1.8.15
222
+ signing_key:
223
+ specification_version: 3
224
+ summary: "%Q{Finite state machine implementation.}"
225
+ test_files: []
226
+