Module: Mongoid::Locker
- Defined in:
- lib/mongoid/locker.rb,
lib/mongoid/locker/version.rb,
lib/mongoid/locker/wrapper2.rb,
lib/mongoid/locker/wrapper3.rb,
lib/mongoid/locker/wrapper4.rb,
lib/mongoid/locker/wrapper5.rb,
lib/mongoid/locker/wrapper6.rb
Defined Under Namespace
Modules: ClassMethods, Wrapper Classes: LockError
Constant Summary collapse
- VERSION =
'0.3.5'
Class Method Summary collapse
- .included(mod) ⇒ Object private
Instance Method Summary collapse
-
#has_lock? ⇒ Boolean
Returns whether the current instance has the lock or not.
-
#locked? ⇒ Boolean
Returns whether the document is currently locked or not.
-
#with_lock(opts = {}) ⇒ void
Primary method of plugin: execute the provided code once the document has been successfully locked.
Class Method Details
.included(mod) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
42 43 44 45 46 47 |
# File 'lib/mongoid/locker.rb', line 42 def self.included(mod) mod.extend ClassMethods mod.field :locked_at, type: Time mod.field :locked_until, type: Time end |
Instance Method Details
#has_lock? ⇒ Boolean
Returns whether the current instance has the lock or not.
59 60 61 |
# File 'lib/mongoid/locker.rb', line 59 def has_lock? !!(@has_lock && self.locked?) end |
#locked? ⇒ Boolean
Returns whether the document is currently locked or not.
52 53 54 |
# File 'lib/mongoid/locker.rb', line 52 def locked? !!(locked_until && locked_until > Time.now) end |
#with_lock(opts = {}) ⇒ void
This method returns an undefined value.
Primary method of plugin: execute the provided code once the document has been successfully locked.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mongoid/locker.rb', line 72 def with_lock(opts = {}) had_lock = self.has_lock? unless had_lock opts[:retries] = 1 if opts[:wait] lock(opts) end begin yield ensure unlock if locked? && !had_lock end end |