Module: Workarea::Lockable
- Included in:
- Order
- Defined in:
- app/models/workarea/lockable.rb
Instance Method Summary collapse
-
#default_lock_value ⇒ String
The value set on the lock unless otherwise specified.
-
#lock!(options = {}) ⇒ Boolean
Obtain a lock.
-
#lock_key ⇒ String
The key used to obtain a lock for this object.
-
#locked? ⇒ Boolean
Check if there is a lock for this object.
-
#unlock!(value: nil) ⇒ Boolean
Release the lock.
Instance Method Details
#default_lock_value ⇒ String
The value set on the lock unless otherwise specified. Derived from the lock_key, the object id of the instance of the Lockable object, and the microseconds since the epoch. A unique key on each instance ensures the instance can unlock itself but cannot be unlocked by any other instance.
20 21 22 23 |
# File 'app/models/workarea/lockable.rb', line 20 def default_lock_value @default_lock_value ||= "#{lock_key}/#{object_id}/#{DateTime.current.strftime("%Q")}" end |
#lock!(options = {}) ⇒ Boolean
Obtain a lock.
42 43 44 45 |
# File 'app/models/workarea/lockable.rb', line 42 def lock!( = {}) value = .delete(:value) || default_lock_value Lock.create!(lock_key, value, ) end |
#lock_key ⇒ String
The key used to obtain a lock for this object. Requires the object responds to #id. Remains consistent across instances of the same object.
8 9 10 |
# File 'app/models/workarea/lockable.rb', line 8 def lock_key "#{self.class.name.underscore}/#{id}/lock" end |