locky 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3175be064223f8910885f31dedef146ee2dcfd6d
4
- data.tar.gz: 4b60fb8230056e04d5bff8865df94b5408a2e8d8
3
+ metadata.gz: 722dc396916ebc5c1f3fb90dd0b598fa2bc3ad44
4
+ data.tar.gz: 2a3961bf97cd47f5d7c20fb73de8110418e8957d
5
5
  SHA512:
6
- metadata.gz: 08efe2542304e3b02a4c55752d9d8520f954a343782c0e1ba97ba58d9167a1298fcb8bd2a41dd8adb2df40d64e645b407085998f4e1fd4ea9c2b8d3362034d55
7
- data.tar.gz: b92a1f667ff1d26e743397ec50e11fb1db2a9a620384150194c81f0de83da92736f5aaada1a6dc2320647ff35b0578e7626c95842f7b0557172ccfdcdf515fb0
6
+ metadata.gz: cd062d5aa843801664363763366fcbcc2df848604c05cb59e082fc3465bce64fc55e1e51b30d74d5c08702873a9a8089ff356a5e1f46d898a691c58653611743
7
+ data.tar.gz: 531cf00636ec388d40c4b5f02fdd9f8c83ebb5b97de49b34c583fc60a4740547b5b032a3e5c11a394b794a43591d92b3eb2de9417dbc04c6ff861bdff8b67dbc
@@ -1,34 +1,73 @@
1
1
  class Locky
2
2
 
3
- Error = Class.new StandardError
4
-
3
+ class Error < StandardError
4
+ def initialize(locker)
5
+ @locker = locker
6
+ end
7
+
8
+ def message
9
+ "#{@locker.name} already locked by #{@locker.locked_by}"
10
+ end
11
+ end
12
+
5
13
  attr_reader :name
6
14
 
7
- def initialize(name, adapter={})
15
+ def initialize(name, storage={})
8
16
  @name = name
9
- @adapter = adapter
17
+ @storage = storage
10
18
  end
11
19
 
12
20
  def lock(process)
13
- raise Error, "#{name} already locked by #{locked_by}" if locked?
14
- adapter[name] = process
21
+ raise Error, self if locked? && locked_by != process
22
+ register process
15
23
  begin
16
24
  yield
17
25
  ensure
18
- adapter.delete name
26
+ unregister
19
27
  end
20
28
  end
21
29
 
30
+ def lock!(process)
31
+ raise Error, self if locked?
32
+ lock(process) { yield }
33
+ end
34
+
35
+ def unlock!
36
+ storage.delete name
37
+ storage.delete counter
38
+ end
39
+
22
40
  def locked?
23
- adapter.key? name
41
+ storage.key? name
24
42
  end
25
43
 
26
44
  def locked_by
27
- adapter[name]
45
+ storage[name]
28
46
  end
29
47
 
30
48
  private
31
49
 
32
- attr_reader :adapter
50
+ attr_reader :storage
51
+
52
+ def counter
53
+ "#{name}__counter__"
54
+ end
55
+
56
+ def register(process)
57
+ if storage.key? name
58
+ storage[counter] += 1
59
+ else
60
+ storage[name] = process
61
+ storage[counter] = 1
62
+ end
63
+ end
64
+
65
+ def unregister
66
+ if storage[counter] > 1
67
+ storage[counter] -= 1
68
+ else
69
+ unlock!
70
+ end
71
+ end
33
72
 
34
73
  end
@@ -1,3 +1,3 @@
1
1
  class Locky
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -2,9 +2,10 @@ require 'minitest_helper'
2
2
 
3
3
  describe Locky do
4
4
 
5
- let(:locker) { Locky.new :test }
6
-
7
- it 'Test' do
5
+ let(:storage) { Hash.new }
6
+ let(:locker) { Locky.new :test, storage }
7
+
8
+ it 'Flexible' do
8
9
  locker.name.must_equal :test
9
10
  locker.wont_be :locked?
10
11
 
@@ -12,6 +13,8 @@ describe Locky do
12
13
  locker.must_be :locked?
13
14
  locker.locked_by.must_equal :process_1
14
15
 
16
+ locker.lock(:process_1) { }
17
+
15
18
  error = proc { locker.lock :process_2 }.must_raise Locky::Error
16
19
  error.message.must_equal 'test already locked by process_1'
17
20
  end
@@ -19,4 +22,30 @@ describe Locky do
19
22
  locker.wont_be :locked?
20
23
  end
21
24
 
25
+ it 'Strict' do
26
+ locker.name.must_equal :test
27
+ locker.wont_be :locked?
28
+
29
+ locker.lock! :process_1 do
30
+ locker.must_be :locked?
31
+ locker.locked_by.must_equal :process_1
32
+
33
+ error = proc { locker.lock! :process_1 }.must_raise Locky::Error
34
+ error.message.must_equal 'test already locked by process_1'
35
+
36
+ error = proc { locker.lock :process_2 }.must_raise Locky::Error
37
+ error.message.must_equal 'test already locked by process_1'
38
+ end
39
+
40
+ locker.wont_be :locked?
41
+ end
42
+
43
+ it 'Force unlock' do
44
+ storage[locker.name] = :process_1
45
+
46
+ locker.must_be :locked?
47
+ locker.unlock!
48
+ locker.wont_be :locked?
49
+ end
50
+
22
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-06 00:00:00.000000000 Z
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler