Module: Mongoid::Locker
- Defined in:
- lib/mongoid/locker.rb,
lib/mongoid/locker/version.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 =
'1.0.1'.freeze
Class Attribute Summary collapse
-
.locked_at_field ⇒ Object
Returns the value of attribute locked_at_field.
-
.locked_until_field ⇒ Object
Returns the value of attribute locked_until_field.
Class Method Summary collapse
-
.configure {|_self| ... } ⇒ Object
Sets configuration using a block.
- .included(mod) ⇒ Object private
-
.reset! ⇒ Object
Resets to default configuration.
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 Attribute Details
.locked_at_field ⇒ Object
Returns the value of attribute locked_at_field.
62 63 64 |
# File 'lib/mongoid/locker.rb', line 62 def locked_at_field @locked_at_field end |
.locked_until_field ⇒ Object
Returns the value of attribute locked_until_field.
62 63 64 |
# File 'lib/mongoid/locker.rb', line 62 def locked_until_field @locked_until_field end |
Class Method Details
.configure {|_self| ... } ⇒ Object
Sets configuration using a block
Mongoid::Locker.configure do |config|
config.locked_at_field = :mongoid_locker_locked_at
config.locked_until_field = :mongoid_locker_locked_until
end
81 82 83 |
# File 'lib/mongoid/locker.rb', line 81 def configure yield(self) if block_given? end |
.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.
65 66 67 68 69 70 71 72 73 |
# File 'lib/mongoid/locker.rb', line 65 def included(mod) mod.extend ClassMethods mod.class_variable_set(:@@locked_at_field, locked_at_field) mod.class_variable_set(:@@locked_until_field, locked_until_field) mod.send(:define_method, :locked_at_field) { mod.class_variable_get(:@@locked_at_field) } mod.send(:define_method, :locked_until_field) { mod.class_variable_get(:@@locked_until_field) } end |
.reset! ⇒ Object
Resets to default configuration.
86 87 88 89 90 |
# File 'lib/mongoid/locker.rb', line 86 def reset! # The field names used by default. @locked_at_field = :locked_at @locked_until_field = :locked_until end |
Instance Method Details
#has_lock? ⇒ Boolean
Returns whether the current instance has the lock or not.
103 104 105 |
# File 'lib/mongoid/locker.rb', line 103 def has_lock? !!(@has_lock && locked?) end |
#locked? ⇒ Boolean
Returns whether the document is currently locked or not.
96 97 98 |
# File 'lib/mongoid/locker.rb', line 96 def locked? !!(self[locked_until_field] && self[locked_until_field] > Time.now.utc) 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.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mongoid/locker.rb', line 116 def with_lock(opts = {}) unless !persisted? || (had_lock = has_lock?) if opts[:wait] opts[:retries] = 1 warn 'WARN: `:wait` option for Mongoid::Locker is deprecated - use `retries: 1` instead.' end lock(opts) end begin yield ensure unlock if !had_lock && locked? end end |