maintain 0.2.11 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.11
1
+ 0.2.12
@@ -9,9 +9,25 @@ module Maintain
9
9
  def on(maintainee, attribute, event, state, method, options)
10
10
  attribute_check = "#{attribute}#{"_was" if event == :exit}_#{state}?"
11
11
  maintainee.before_save method, :if => lambda {|instance|
12
- instance.send("#{attribute}_changed?") && instance.send(attribute_check) &&
13
- (!options[:if] || (options[:if].is_a?(Proc) && instance.instance_eval(&options[:unless])) || instance.send(options[:if])) &&
14
- (!options[:unless] || !(options[:unless].is_a?(Proc) && instance.instance_eval(&options[:unless])) || !instance.send(options[:unless]))
12
+ if instance.send("#{attribute}_changed?") && instance.send(attribute_check)
13
+ if options[:if]
14
+ if options[:if].is_a?(Proc)
15
+ instance.instance_eval(&options[:if])
16
+ else
17
+ instance.send(options[:if])
18
+ end
19
+ elsif options[:unless]
20
+ if options[:unless].is_a?(Proc)
21
+ !instance.instance_eval(&options[:unless])
22
+ else
23
+ !instance.send(options[:unless])
24
+ end
25
+ else
26
+ true
27
+ end
28
+ else
29
+ false
30
+ end
15
31
  }
16
32
  end
17
33
 
data/maintain.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{maintain}
8
- s.version = "0.2.10"
8
+ s.version = "0.2.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Flip Sasser"]
12
- s.date = %q{2011-06-03}
12
+ s.date = %q{2011-07-13}
13
13
  s.description = %q{
14
14
  Maintain is a simple state machine mixin for Ruby objects. It supports comparisons, bitmasks,
15
15
  and hooks that really work. It can be used for multiple attributes and will always do its best to
@@ -129,7 +129,8 @@ if proceed
129
129
 
130
130
  on :enter, :old, :do_something
131
131
  on :exit, :foo, :do_something_else
132
-
132
+ on :enter, :bar, lambda { hello! }, :if => :run_hello?
133
+ on :exit, :bar, lambda { hello! }, :unless => :run_hello?
133
134
  end
134
135
 
135
136
  ActiveMaintainTest.class_eval do
@@ -140,6 +141,18 @@ if proceed
140
141
  def do_something_else
141
142
  # Do something else!
142
143
  end
144
+
145
+ def hello!
146
+
147
+ end
148
+
149
+ def run_hello!
150
+ @run_hello = !@run_hello# ? false : true
151
+ end
152
+
153
+ def run_hello?
154
+ @run_hello
155
+ end
143
156
  end
144
157
  end
145
158
 
@@ -164,6 +177,38 @@ if proceed
164
177
  active_maintain_test.status = :new
165
178
  active_maintain_test.save
166
179
  end
180
+
181
+ it "should not run the :bar enter hook if run_hello? returns false" do
182
+ active_maintain_test = ActiveMaintainTest.new
183
+ active_maintain_test.should_not_receive(:hello!)
184
+ active_maintain_test.status = :bar
185
+ active_maintain_test.save
186
+ end
187
+
188
+ it "should run the :bar enter hook if run_hello? returns true" do
189
+ active_maintain_test = ActiveMaintainTest.new
190
+ active_maintain_test.run_hello!
191
+ active_maintain_test.should_receive(:hello!)
192
+ active_maintain_test.status = :bar
193
+ active_maintain_test.save
194
+ end
195
+
196
+ it "should not run the :bar exit hook if run_hello? returns true" do
197
+ active_maintain_test = ActiveMaintainTest.create(:status => :bar)
198
+ active_maintain_test.run_hello!
199
+ active_maintain_test.run_hello?.should be_true
200
+ active_maintain_test.should_not_receive(:hello!)
201
+ active_maintain_test.status = :foo
202
+ active_maintain_test.save
203
+ end
204
+
205
+ it "should run the :bar exit hook if run_hello? returns false" do
206
+ active_maintain_test = ActiveMaintainTest.create(:status => :bar)
207
+ active_maintain_test.run_hello?.should be_false
208
+ active_maintain_test.should_receive(:hello!)
209
+ active_maintain_test.status = :foo
210
+ active_maintain_test.save
211
+ end
167
212
  end
168
213
 
169
214
  describe "named_scopes" do
@@ -199,5 +244,23 @@ if proceed
199
244
  end
200
245
 
201
246
  end
247
+
248
+ describe "serialization" do
249
+ before :all do
250
+ ActiveMaintainTest.maintain :status do
251
+ state :new, :default => true
252
+ state :old
253
+ state :foo
254
+ state :bar
255
+ aggregate :everything, :as => [:new, :old, :foo, :bar]
256
+ aggregate :fakes, :as => [:foo, :bar]
257
+ end
258
+ end
259
+
260
+ it "should not screw with to_json" do
261
+ foo = ActiveMaintainTest.create
262
+ foo.as_json.should == {:active_maintain_test => {:id => foo.id, :permissions => 0, :status => :new}.stringify_keys}.stringify_keys
263
+ end
264
+ end
202
265
  end
203
266
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maintain
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 11
10
- version: 0.2.11
9
+ - 12
10
+ version: 0.2.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Flip Sasser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-23 00:00:00 -04:00
18
+ date: 2011-07-13 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21