Skip to content

Commit

Permalink
Don't use InstanceMethods since ActiveSupport::Concern deprecated it.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpr5 committed Aug 25, 2012
1 parent 0775f51 commit c3dcfcc
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions lib/mongo/locking/model_methods.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,49 +57,44 @@ def locker


end # ClassMethods end # ClassMethods


module InstanceMethods # Main closure-based lock method. opts remains present for

# future utility.
# Main closure-based lock method. opts remains present for def lock(opts = {})
# future utility. raise ArgumentError, "#{self.class.name}#lock requires a block" unless block_given?
def lock(opts = {})
raise ArgumentError, "#{self.class.name}#lock requires a block" unless block_given?

# Look for the top level lockable
lockable = self.class.locker.root_for(self)

# Try to acquire the lock. If succeeds, "locked" will be set
locked = lockable.class.locker.acquire(lockable)

return yield

rescue Mongo::Locking::Exceptions::Error => e
Locking.error "#{self.class.name}#lock failed"
raise e

ensure
# Only unlock if "locked" was set. We're using this to
# distinguish between an exception from the yield vs. an
# exception from our own locking code. Doing it in an
# ensure block makes us defensible against a return from
# within the closure, too.
#
# Calling lockable's locker instead of self potentially
# saves us the cost of "find root lockable" that locker
# would perform.
lockable.class.locker.release(lockable) if locked
end


# Return true if operating within an open acquired lock. # Look for the top level lockable
def have_lock? lockable = self.class.locker.root_for(self)
lockable = self.class.locker.root_for(self)
locker = lockable.class.locker # Try to acquire the lock. If succeeds, "locked" will be set
key = locker.key_for(lockable) locked = lockable.class.locker.acquire(lockable)
return locker.refcounts[key] > 0
end return yield


end # InstanceMethods rescue Mongo::Locking::Exceptions::Error => e
Locking.error "#{self.class.name}#lock failed"
raise e

ensure
# Only unlock if "locked" was set. We're using this to
# distinguish between an exception from the yield vs. an
# exception from our own locking code. Doing it in an
# ensure block makes us defensible against a return from
# within the closure, too.
#
# Calling lockable's locker instead of self potentially
# saves us the cost of "find root lockable" that locker
# would perform.
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 # ModelMethods end # ModelMethods
end # Locking end # Locking
end # Mongo end # Mongo

0 comments on commit c3dcfcc

Please sign in to comment.