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.1'

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



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

Returns:

  • (Boolean)


20
21
22
# File 'lib/locky.rb', line 20

def locked?
  adapter.key? name
end

#locked_byObject



24
25
26
# File 'lib/locky.rb', line 24

def locked_by
  adapter[name]
end