Class: Locky

Inherits:
Object
  • Object
show all
Defined in:
lib/locky.rb,
lib/locky/version.rb

Constant Summary collapse

Error =
Class.new StandardError
VERSION =
'0.0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#nameObject (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

Raises:



12
13
14
15
16
17
18
19
20
# File 'lib/locky.rb', line 12

def lock(process)
  raise Error, "#{name} already locked by #{locked_by}" if locked?
  adapter[name] = process
  begin
    yield
  ensure
    adapter.delete name
  end
end

#locked?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/locky.rb', line 22

def locked?
  adapter.key? name
end

#locked_byObject



26
27
28
# File 'lib/locky.rb', line 26

def locked_by
  adapter[name]
end