Class: Locky
- Inherits:
-
Object
- Object
- Locky
- Defined in:
- lib/locky.rb,
lib/locky/version.rb
Constant Summary collapse
- Error =
Class.new StandardError
- VERSION =
'0.0.1'
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, adapter = {}) ⇒ Locky
constructor
A new instance of Locky.
- #lock(process) ⇒ Object
- #locked? ⇒ Boolean
- #locked_by ⇒ Object
Constructor Details
#initialize(name, adapter = {}) ⇒ Locky
Returns a new instance of Locky.
7 8 9 10 |
# File 'lib/locky.rb', line 7 def initialize(name, adapter={}) @name = name @adapter = adapter end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/locky.rb', line 5 def name @name end |
Instance Method Details
#lock(process) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/locky.rb', line 12 def lock(process) raise Error, "#{name} already locked by #{locked_by}" if locked? adapter[name] = process yield ensure adapter.delete name end |
#locked? ⇒ Boolean
20 21 22 |
# File 'lib/locky.rb', line 20 def locked? adapter.key? name end |
#locked_by ⇒ Object
24 25 26 |
# File 'lib/locky.rb', line 24 def locked_by adapter[name] end |