lib/mongoid/locker.rb in mongoid-locker-0.3.4 vs lib/mongoid/locker.rb in mongoid-locker-0.3.5
- old
+ new
@@ -1,5 +1,6 @@
+require File.expand_path(File.join(File.dirname(__FILE__), 'locker', 'version'))
require File.expand_path(File.join(File.dirname(__FILE__), 'locker', 'wrapper'))
module Mongoid
module Locker
# Error thrown if document could not be successfully locked.
@@ -67,21 +68,21 @@
# @option opts [Float] :retry_sleep How long to sleep between attempts to acquire lock - defaults to time left until lock is available
# @option opts [Boolean] :wait If the document is currently locked, wait until the lock expires and try again - defaults to false. If set, :retries will be ignored
# @option opts [Boolean] :reload After acquiring the lock, reload the document - defaults to true
# @return [void]
def with_lock(opts = {})
- have_lock = self.has_lock?
+ had_lock = self.has_lock?
- unless have_lock
+ unless had_lock
opts[:retries] = 1 if opts[:wait]
lock(opts)
end
begin
yield
ensure
- unlock unless have_lock
+ unlock if locked? && !had_lock
end
end
protected
@@ -159,11 +160,10 @@
locked_until: nil
}
)
- self.locked_at = nil
- self.locked_until = nil
+ self.attributes = { locked_at: nil, locked_until: nil } unless destroyed?
@has_lock = false
end
end
end