Browse files

Added Mongo::Locking::ModelMethods#have_lock? predicate.

  - This tells you if you're inside a lock block i.e. have acquired a lock and
    it is not released yet.
  • Loading branch information...
1 parent d4885d4 commit 782af826bd405b40a0618565b75f471f09c753d1 @brendan brendan committed Jul 22, 2011
Showing with 8 additions and 0 deletions.
  1. +8 −0 lib/mongo/locking/model_methods.rb
View
8 lib/mongo/locking/model_methods.rb
@@ -89,6 +89,14 @@ def lock(opts = {})
lockable.class.locker.release(lockable) if locked
end
+ # Return true if operating within an open acquired lock.
+ def have_lock?
+ lockable = self.class.locker.root_for(self)
+ locker = lockable.class.locker
+ key = locker.key_for(lockable)
+ return locker.refcounts[key] == 0
+ end
+
end # InstanceMethods
end # ModelMethods

0 comments on commit 782af82

Please sign in to comment.