Class: Locky
- Inherits:
-
Object
- Object
- Locky
- Defined in:
- lib/locky.rb,
lib/locky/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'0.0.3'
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, storage = {}) ⇒ Locky
constructor
A new instance of Locky.
- #lock(process) ⇒ Object
- #lock!(process) ⇒ Object
- #locked? ⇒ Boolean
- #locked_by ⇒ Object
- #unlock! ⇒ Object
Constructor Details
#initialize(name, storage = {}) ⇒ Locky
Returns a new instance of Locky.
15 16 17 18 |
# File 'lib/locky.rb', line 15 def initialize(name, storage={}) @name = name @storage = storage end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/locky.rb', line 13 def name @name end |
Instance Method Details
#lock(process) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/locky.rb', line 20 def lock(process) raise Error, self if locked? && locked_by != process register process begin yield ensure unregister end end |
#lock!(process) ⇒ Object
30 31 32 33 |
# File 'lib/locky.rb', line 30 def lock!(process) raise Error, self if locked? lock(process) { yield } end |
#locked? ⇒ Boolean
40 41 42 |
# File 'lib/locky.rb', line 40 def locked? storage.key? name end |
#locked_by ⇒ Object
44 45 46 |
# File 'lib/locky.rb', line 44 def locked_by storage[name] end |
#unlock! ⇒ Object
35 36 37 38 |
# File 'lib/locky.rb', line 35 def unlock! storage.delete name storage.delete counter end |