maintain 0.2.23 → 0.3.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.
- checksums.yaml +7 -0
- data/CHANGES.md +34 -0
- data/README.markdown +9 -9
- data/lib/maintain.rb +42 -27
- data/lib/maintain/backend.rb +2 -12
- data/lib/maintain/backend/active_record.rb +17 -17
- data/lib/maintain/backend/data_mapper.rb +1 -1
- data/lib/maintain/bitmask_value.rb +7 -2
- data/lib/maintain/maintainer.rb +69 -41
- data/lib/maintain/value.rb +4 -3
- data/spec/active_record_spec.rb +75 -63
- data/spec/bitwise_spec.rb +4 -4
- data/spec/class_methods_spec.rb +2 -2
- data/spec/comparing_state_spec.rb +14 -14
- data/spec/data_mapper_spec.rb +10 -10
- data/spec/defining_states_spec.rb +8 -8
- data/spec/hooks_spec.rb +4 -4
- data/spec/integer_spec.rb +3 -3
- data/spec/maintain_spec.rb +2 -2
- data/spec/object_spec.rb +1 -1
- data/spec/proxy_spec.rb +6 -6
- data/spec/setting_state_spec.rb +1 -1
- metadata +30 -21
- data/.rspec +0 -1
- data/CHANGES +0 -27
- data/Rakefile +0 -18
- data/VERSION +0 -1
- data/autotest/discover.rb +0 -4
- data/maintain.gemspec +0 -66
data/lib/maintain/value.rb
CHANGED
@@ -88,7 +88,8 @@ module Maintain
|
|
88
88
|
self == #{value_name.inspect}
|
89
89
|
end
|
90
90
|
EOC
|
91
|
-
# Calling `method` on ourselves fails. Something to do
|
91
|
+
# Calling `method` on ourselves fails. Something to do
|
92
|
+
# w/subclasses. Meh.
|
92
93
|
return self == value_name
|
93
94
|
when '!'
|
94
95
|
self.class.class_eval <<-EOC
|
@@ -125,7 +126,7 @@ module Maintain
|
|
125
126
|
end
|
126
127
|
|
127
128
|
def state_value_for(state, value)
|
128
|
-
if
|
129
|
+
if state.is_a?(String) || state.is_a?(Symbol)
|
129
130
|
if !state.to_s.strip.empty? && state_hash = @state.states[state.to_sym]
|
130
131
|
state_hash[value]
|
131
132
|
else
|
@@ -136,4 +137,4 @@ module Maintain
|
|
136
137
|
end
|
137
138
|
end
|
138
139
|
end
|
139
|
-
end
|
140
|
+
end
|
data/spec/active_record_spec.rb
CHANGED
@@ -1,34 +1,21 @@
|
|
1
1
|
# Some specs to check against ActiveRecord conflicts. Rails tends to blow
|
2
2
|
# shit up when you build it outside of Rails. We'll see how this goes...
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
gem 'activerecord', '>= 2.3.5'
|
8
|
-
require 'active_record'
|
9
|
-
require 'logger'
|
10
|
-
proceed = true
|
11
|
-
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
12
|
-
ActiveRecord::Base.logger.level = Logger::Severity::UNKNOWN
|
13
|
-
rescue Gem::LoadError, LoadError
|
14
|
-
puts 'Not testing ActiveRecord (unavailable)'
|
15
|
-
end
|
16
|
-
|
17
|
-
if proceed
|
18
|
-
# Use load to have it evaluate the ActiveRecord::Base extension logic again, in the event
|
19
|
-
# that we've already done that with a previous test.
|
4
|
+
active_record_tests = lambda do |version|
|
5
|
+
# Use load to have it evaluate the ActiveRecord::Base extension logic again,
|
6
|
+
# in the event that we've already done that with a previous test.
|
20
7
|
load 'maintain.rb'
|
21
|
-
describe Maintain, "ActiveRecord::Base" do
|
8
|
+
describe Maintain, "ActiveRecord::Base version #{version}" do
|
22
9
|
it "should automatically be extended" do
|
23
10
|
ActiveRecord::Base.should respond_to(:maintain)
|
24
11
|
end
|
25
12
|
|
26
13
|
before :each do
|
27
|
-
ActiveRecord::Base.establish_connection({:
|
14
|
+
ActiveRecord::Base.establish_connection({adapter: 'sqlite3', database: ':memory:', pool: 5, timeout: 5000})
|
28
15
|
class ::ActiveMaintainTest < ActiveRecord::Base; end
|
29
16
|
silence_stream(STDOUT) do
|
30
17
|
ActiveRecord::Schema.define do
|
31
|
-
create_table :active_maintain_tests, :
|
18
|
+
create_table :active_maintain_tests, force: true do |t|
|
32
19
|
t.string :status
|
33
20
|
t.integer :permissions
|
34
21
|
end
|
@@ -39,12 +26,12 @@ if proceed
|
|
39
26
|
describe "accessors" do
|
40
27
|
before :all do
|
41
28
|
ActiveMaintainTest.maintain :status do
|
42
|
-
state :new, :
|
29
|
+
state :new, default: true
|
43
30
|
state :old
|
44
31
|
state :foo
|
45
32
|
state :bar
|
46
|
-
aggregate :everything, :
|
47
|
-
aggregate :fakes, :
|
33
|
+
aggregate :everything, as: [:new, :old, :foo, :bar]
|
34
|
+
aggregate :fakes, as: [:foo, :bar]
|
48
35
|
end
|
49
36
|
end
|
50
37
|
|
@@ -54,7 +41,7 @@ if proceed
|
|
54
41
|
end
|
55
42
|
|
56
43
|
it "should allow us to update its status to 'old'" do
|
57
|
-
active_maintain_test = ActiveMaintainTest.new(:
|
44
|
+
active_maintain_test = ActiveMaintainTest.new(status: 'old')
|
58
45
|
active_maintain_test.status.should == 'old'
|
59
46
|
lambda {
|
60
47
|
active_maintain_test.save!
|
@@ -64,7 +51,7 @@ if proceed
|
|
64
51
|
|
65
52
|
it "should allow us to update statuses using update_attributes" do
|
66
53
|
active_maintain_test = ActiveMaintainTest.new
|
67
|
-
active_maintain_test.update_attributes(:
|
54
|
+
active_maintain_test.update_attributes(status: :bar)
|
68
55
|
ActiveMaintainTest.first.status.should == :bar
|
69
56
|
end
|
70
57
|
|
@@ -75,66 +62,73 @@ if proceed
|
|
75
62
|
end
|
76
63
|
|
77
64
|
it "should return the correct name when told to" do
|
78
|
-
|
65
|
+
ActiveMaintainTest.create!(status: 'old')
|
79
66
|
ActiveMaintainTest.first.status.name.should == 'old'
|
80
67
|
end
|
68
|
+
|
69
|
+
it "sets the value with a bang method" do
|
70
|
+
active_maintain_test = ActiveMaintainTest.create!
|
71
|
+
active_maintain_test.bar!
|
72
|
+
active_maintain_test.save!
|
73
|
+
ActiveMaintainTest.first.status.should == :bar
|
74
|
+
end
|
81
75
|
end
|
82
76
|
|
83
77
|
describe "bitmasks" do
|
84
78
|
before :all do
|
85
|
-
ActiveMaintainTest.maintain :permissions, :
|
79
|
+
ActiveMaintainTest.maintain :permissions, bitmask: true do
|
86
80
|
state :add, 0
|
87
81
|
state :delete, 1
|
88
82
|
state :foo, 2
|
89
83
|
state :bar, 3
|
90
84
|
|
91
|
-
aggregate :everything, :
|
92
|
-
aggregate :fakes, :
|
85
|
+
aggregate :everything, as: [:new, :old, :foo, :bar]
|
86
|
+
aggregate :fakes, as: [:foo, :bar]
|
93
87
|
end
|
94
88
|
end
|
95
89
|
|
96
90
|
it "should allow me to set a bitmask value" do
|
97
|
-
active_maintain_test = ActiveMaintainTest.create(:
|
98
|
-
|
91
|
+
active_maintain_test = ActiveMaintainTest.create(permissions: 'add')
|
92
|
+
active_maintain_test.permissions.add?.should be_true
|
99
93
|
end
|
100
94
|
|
101
95
|
it "should allow me to set multiple bitmask values" do
|
102
|
-
active_maintain_test = ActiveMaintainTest.create(:
|
103
|
-
|
104
|
-
|
96
|
+
active_maintain_test = ActiveMaintainTest.create(permissions: ['add', 'delete'])
|
97
|
+
active_maintain_test.permissions.add?.should be_true
|
98
|
+
active_maintain_test.permissions.delete?.should be_true
|
105
99
|
end
|
106
100
|
|
107
101
|
it "should allow me to set a blank string as bitmask values" do
|
108
|
-
active_maintain_test = ActiveMaintainTest.create(:
|
109
|
-
|
102
|
+
active_maintain_test = ActiveMaintainTest.create(permissions: '')
|
103
|
+
active_maintain_test.permissions.should == 0
|
110
104
|
end
|
111
105
|
|
112
106
|
it "should allow me to set an empty array as bitmask values" do
|
113
|
-
active_maintain_test = ActiveMaintainTest.create(:
|
114
|
-
|
107
|
+
active_maintain_test = ActiveMaintainTest.create(permissions: [])
|
108
|
+
active_maintain_test.permissions.should == 0
|
115
109
|
end
|
116
110
|
|
117
111
|
it "should allow me to set an array with empty strings as bitmask values" do
|
118
|
-
active_maintain_test = ActiveMaintainTest.create(:
|
119
|
-
|
112
|
+
active_maintain_test = ActiveMaintainTest.create(permissions: [''])
|
113
|
+
active_maintain_test.permissions.should == 0
|
120
114
|
end
|
121
115
|
end
|
122
116
|
|
123
117
|
describe "hooks" do
|
124
118
|
before :all do
|
125
119
|
ActiveMaintainTest.maintain :status do
|
126
|
-
state :new, :
|
120
|
+
state :new, default: true
|
127
121
|
state :old
|
128
122
|
state :foo
|
129
123
|
state :bar
|
130
|
-
aggregate :everything, :
|
131
|
-
aggregate :fakes, :
|
124
|
+
aggregate :everything, as: [:new, :old, :foo, :bar]
|
125
|
+
aggregate :fakes, as: [:foo, :bar]
|
132
126
|
|
133
127
|
on :enter, :old, :do_something
|
134
128
|
on :exit, :foo, :do_something_else
|
135
|
-
on :enter, :bar, lambda { hello! }, :
|
136
|
-
on :exit, :bar, lambda { hello! }, :
|
137
|
-
on :enter, :bar, :show_my_id, :
|
129
|
+
on :enter, :bar, lambda { hello! }, if: :run_hello?
|
130
|
+
on :exit, :bar, lambda { hello! }, unless: :run_hello?
|
131
|
+
on :enter, :bar, :show_my_id, after: true
|
138
132
|
end
|
139
133
|
|
140
134
|
ActiveMaintainTest.class_eval do
|
@@ -147,11 +141,11 @@ if proceed
|
|
147
141
|
end
|
148
142
|
|
149
143
|
def hello!
|
150
|
-
|
144
|
+
|
151
145
|
end
|
152
146
|
|
153
147
|
def run_hello!
|
154
|
-
@run_hello = !@run_hello
|
148
|
+
@run_hello = !@run_hello
|
155
149
|
end
|
156
150
|
|
157
151
|
def run_hello?
|
@@ -159,7 +153,7 @@ if proceed
|
|
159
153
|
end
|
160
154
|
|
161
155
|
def show_my_id
|
162
|
-
|
156
|
+
logger.info id
|
163
157
|
end
|
164
158
|
end
|
165
159
|
end
|
@@ -202,7 +196,7 @@ if proceed
|
|
202
196
|
end
|
203
197
|
|
204
198
|
it "should not run the :bar exit hook if run_hello? returns true" do
|
205
|
-
active_maintain_test = ActiveMaintainTest.create(:
|
199
|
+
active_maintain_test = ActiveMaintainTest.create(status: :bar)
|
206
200
|
active_maintain_test.run_hello!
|
207
201
|
active_maintain_test.run_hello?.should be_true
|
208
202
|
active_maintain_test.should_not_receive(:hello!)
|
@@ -211,7 +205,7 @@ if proceed
|
|
211
205
|
end
|
212
206
|
|
213
207
|
it "should run the :bar exit hook if run_hello? returns false" do
|
214
|
-
active_maintain_test = ActiveMaintainTest.create(:
|
208
|
+
active_maintain_test = ActiveMaintainTest.create(status: :bar)
|
215
209
|
active_maintain_test.run_hello?.should be_false
|
216
210
|
active_maintain_test.should_receive(:hello!)
|
217
211
|
active_maintain_test.status = :foo
|
@@ -219,9 +213,9 @@ if proceed
|
|
219
213
|
end
|
220
214
|
|
221
215
|
it "should ONLY run the :bar / :show_my_id exit hook AFTER the record is saved" do
|
222
|
-
active_maintain_test = ActiveMaintainTest.new(:
|
216
|
+
active_maintain_test = ActiveMaintainTest.new(status: :foo)
|
223
217
|
active_maintain_test.status = :bar
|
224
|
-
|
218
|
+
ActiveRecord::Base.logger.should_receive(:info).with(1)
|
225
219
|
active_maintain_test.save!
|
226
220
|
end
|
227
221
|
end
|
@@ -229,12 +223,12 @@ if proceed
|
|
229
223
|
describe "named_scopes" do
|
230
224
|
before :all do
|
231
225
|
ActiveMaintainTest.maintain :status do
|
232
|
-
state :new, :
|
226
|
+
state :new, default: true
|
233
227
|
state :old
|
234
228
|
state :foo
|
235
229
|
state :bar
|
236
|
-
aggregate :everything, :
|
237
|
-
aggregate :fakes, :
|
230
|
+
aggregate :everything, as: [:new, :old, :foo, :bar]
|
231
|
+
aggregate :fakes, as: [:foo, :bar]
|
238
232
|
end
|
239
233
|
end
|
240
234
|
|
@@ -250,10 +244,10 @@ if proceed
|
|
250
244
|
|
251
245
|
it "should return the correct collections on aggregates" do
|
252
246
|
ActiveMaintainTest.destroy_all
|
253
|
-
one = ActiveMaintainTest.create(:
|
254
|
-
two = ActiveMaintainTest.create(:
|
255
|
-
three = ActiveMaintainTest.create(:
|
256
|
-
four = ActiveMaintainTest.create(:
|
247
|
+
one = ActiveMaintainTest.create(status: :foo)
|
248
|
+
two = ActiveMaintainTest.create(status: :bar)
|
249
|
+
three = ActiveMaintainTest.create(status: :new)
|
250
|
+
four = ActiveMaintainTest.create(status: :old)
|
257
251
|
ActiveMaintainTest.fakes.should == [one, two]
|
258
252
|
ActiveMaintainTest.everything.should == [one, two, three, four]
|
259
253
|
end
|
@@ -263,21 +257,39 @@ if proceed
|
|
263
257
|
describe "serialization" do
|
264
258
|
before :all do
|
265
259
|
ActiveMaintainTest.maintain :status do
|
266
|
-
state :new, :
|
260
|
+
state :new, default: true
|
267
261
|
state :old
|
268
262
|
state :foo
|
269
263
|
state :bar
|
270
|
-
aggregate :everything, :
|
271
|
-
aggregate :fakes, :
|
264
|
+
aggregate :everything, as: [:new, :old, :foo, :bar]
|
265
|
+
aggregate :fakes, as: [:foo, :bar]
|
272
266
|
end
|
273
267
|
end
|
274
268
|
|
275
269
|
it "should not screw with to_json" do
|
276
270
|
foo = ActiveMaintainTest.create
|
277
|
-
json_hash = {:
|
271
|
+
json_hash = {id: foo.id, permissions: 0, status: :new}.stringify_keys
|
278
272
|
foo.as_json.should == json_hash
|
279
273
|
json_hash.to_json.should == json_hash.to_json
|
280
274
|
end
|
281
275
|
end
|
282
276
|
end
|
283
277
|
end
|
278
|
+
|
279
|
+
active_record_versions = ENV["VERSIONS"] && ENV["VERSIONS"].split(",") || %w(2 3 4)
|
280
|
+
require 'rubygems'
|
281
|
+
active_record_versions.each do |version|
|
282
|
+
begin
|
283
|
+
gem 'activerecord', "< #{version.to_i + 1}"
|
284
|
+
require 'active_record'
|
285
|
+
require 'logger'
|
286
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
287
|
+
ActiveRecord::Base.logger.level = Logger::Severity::UNKNOWN
|
288
|
+
ActiveRecord::Base.include_root_in_json = false
|
289
|
+
puts "Testing ActiveRecord version #{version}"
|
290
|
+
active_record_tests.call(version)
|
291
|
+
rescue Gem::LoadError, LoadError
|
292
|
+
puts "Cannot test ActiveRecord #{version} (unavailable)"
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
data/spec/bitwise_spec.rb
CHANGED
@@ -12,9 +12,9 @@ describe Maintain do
|
|
12
12
|
|
13
13
|
describe "bitmask", "class methods" do
|
14
14
|
it "should allow multiple defaults" do
|
15
|
-
MaintainTest.maintain :permissions, :
|
16
|
-
state :edit, 1, :
|
17
|
-
state :delete, 2, :
|
15
|
+
MaintainTest.maintain :permissions, bitmask: true do
|
16
|
+
state :edit, 1, default: true
|
17
|
+
state :delete, 2, default: true
|
18
18
|
state :update, 3
|
19
19
|
end
|
20
20
|
@maintainer = MaintainTest.new
|
@@ -26,7 +26,7 @@ describe Maintain do
|
|
26
26
|
|
27
27
|
describe "bitmask", "instance methods" do
|
28
28
|
before :each do
|
29
|
-
MaintainTest.maintain :permissions, :
|
29
|
+
MaintainTest.maintain :permissions, bitmask: true do
|
30
30
|
state :edit, 1
|
31
31
|
state :delete, 2
|
32
32
|
state :update, 3
|
data/spec/class_methods_spec.rb
CHANGED
@@ -21,7 +21,7 @@ describe Maintain do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should provide a hash of key/value stores in an Integer case, too" do
|
24
|
-
MaintainTest.maintain :state_two, :
|
24
|
+
MaintainTest.maintain :state_two, integer: true do
|
25
25
|
state :new, 1
|
26
26
|
state :overdue, 2
|
27
27
|
state :closed, 3
|
@@ -42,4 +42,4 @@ describe Maintain do
|
|
42
42
|
MaintainTest.foo.should == "foo"
|
43
43
|
MaintainTest.maintain_foo.should == [:new, :overdue, :closed]
|
44
44
|
end
|
45
|
-
end
|
45
|
+
end
|
@@ -17,7 +17,7 @@ describe Maintain do
|
|
17
17
|
|
18
18
|
extend Maintain
|
19
19
|
|
20
|
-
maintains :state, :
|
20
|
+
maintains :state, default: :new do
|
21
21
|
state :new
|
22
22
|
state :overdue
|
23
23
|
state :closed
|
@@ -28,7 +28,7 @@ describe Maintain do
|
|
28
28
|
describe "testing" do
|
29
29
|
describe "string states" do
|
30
30
|
before :each do
|
31
|
-
MaintainTest.maintain :state, :
|
31
|
+
MaintainTest.maintain :state, default: :new do
|
32
32
|
state :new
|
33
33
|
state :overdue
|
34
34
|
state :closed
|
@@ -48,7 +48,7 @@ describe Maintain do
|
|
48
48
|
describe "boolean methods" do
|
49
49
|
describe "on the accessor" do
|
50
50
|
it "should work" do
|
51
|
-
MaintainTest.maintain :state, :
|
51
|
+
MaintainTest.maintain :state, default: :new do
|
52
52
|
state :new
|
53
53
|
state :overdue
|
54
54
|
state :closed
|
@@ -69,7 +69,7 @@ describe Maintain do
|
|
69
69
|
|
70
70
|
describe "on the class itself" do
|
71
71
|
it "should work, too" do
|
72
|
-
MaintainTest.maintain :state, :
|
72
|
+
MaintainTest.maintain :state, default: :new do
|
73
73
|
state :new
|
74
74
|
state :overdue
|
75
75
|
state :closed
|
@@ -81,7 +81,7 @@ describe Maintain do
|
|
81
81
|
end
|
82
82
|
|
83
83
|
it "should work with an attribute name prefix, too!" do
|
84
|
-
MaintainTest.maintain :state, :
|
84
|
+
MaintainTest.maintain :state, default: :new do
|
85
85
|
state :new
|
86
86
|
state :overdue
|
87
87
|
state :closed
|
@@ -99,7 +99,7 @@ describe Maintain do
|
|
99
99
|
end
|
100
100
|
|
101
101
|
it "greater than method" do
|
102
|
-
MaintainTest.maintain :state, :
|
102
|
+
MaintainTest.maintain :state, default: :closed do
|
103
103
|
state :new
|
104
104
|
state :overdue
|
105
105
|
state :closed
|
@@ -116,7 +116,7 @@ describe Maintain do
|
|
116
116
|
end
|
117
117
|
|
118
118
|
it "greater-than-or-equal-to method" do
|
119
|
-
MaintainTest.maintain :state, :
|
119
|
+
MaintainTest.maintain :state, default: :closed do
|
120
120
|
state :new
|
121
121
|
state :overdue
|
122
122
|
state :closed
|
@@ -130,7 +130,7 @@ describe Maintain do
|
|
130
130
|
end
|
131
131
|
|
132
132
|
it "less-than-or-equal-to method" do
|
133
|
-
MaintainTest.maintain :state, :
|
133
|
+
MaintainTest.maintain :state, default: :new do
|
134
134
|
state :new
|
135
135
|
state :overdue
|
136
136
|
state :closed
|
@@ -147,7 +147,7 @@ describe Maintain do
|
|
147
147
|
|
148
148
|
describe "identity comparison" do
|
149
149
|
before :each do
|
150
|
-
MaintainTest.maintain :state, :
|
150
|
+
MaintainTest.maintain :state, default: :new do
|
151
151
|
state :new, 1
|
152
152
|
state :overdue, 2
|
153
153
|
state :closed, 3
|
@@ -172,7 +172,7 @@ describe Maintain do
|
|
172
172
|
|
173
173
|
describe "integer states" do
|
174
174
|
before :each do
|
175
|
-
MaintainTest.maintain :state, :
|
175
|
+
MaintainTest.maintain :state, default: :new do
|
176
176
|
state :new, 1
|
177
177
|
state :overdue, 2
|
178
178
|
state :closed, 3
|
@@ -188,7 +188,7 @@ describe Maintain do
|
|
188
188
|
end
|
189
189
|
|
190
190
|
it "greater than method" do
|
191
|
-
MaintainTest.maintain :state, :
|
191
|
+
MaintainTest.maintain :state, default: :closed do
|
192
192
|
state :new, 1
|
193
193
|
state :overdue, 2
|
194
194
|
state :closed, 3
|
@@ -205,7 +205,7 @@ describe Maintain do
|
|
205
205
|
end
|
206
206
|
|
207
207
|
it "greater-than-or-equal-to method" do
|
208
|
-
MaintainTest.maintain :state, :
|
208
|
+
MaintainTest.maintain :state, default: :closed do
|
209
209
|
state :new, 1
|
210
210
|
state :overdue, 2
|
211
211
|
state :closed, 3
|
@@ -219,7 +219,7 @@ describe Maintain do
|
|
219
219
|
end
|
220
220
|
|
221
221
|
it "less-than-or-equal-to method" do
|
222
|
-
MaintainTest.maintain :state, :
|
222
|
+
MaintainTest.maintain :state, default: :new do
|
223
223
|
state :new, 1
|
224
224
|
state :overdue, 2
|
225
225
|
state :closed, 3
|
@@ -233,4 +233,4 @@ describe Maintain do
|
|
233
233
|
end
|
234
234
|
end
|
235
235
|
end
|
236
|
-
end
|
236
|
+
end
|