mlanett-redis-lock 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -42,7 +42,14 @@ This gem adds lock() and unlock() to Redis instances.
42
42
  lock() takes a block and is safer than using lock() and unlock() separately.
43
43
  lock() takes a key and lifetime and optionally a timeout (otherwise defaulting to 10 second).
44
44
 
45
- redis.lock("test") { do_something }
45
+ redis.lock("test") { |lock| do_something }
46
+
47
+ redis.lock("test") do |lock|
48
+ array.each do |entry|
49
+ do_something(entry)
50
+ lock.extend_life(60)
51
+ end
52
+ end
46
53
 
47
54
  ## Problems
48
55
 
data/lib/redis-lock.rb CHANGED
@@ -37,7 +37,7 @@ class Redis
37
37
  do_lock_with_timeout(timeout) or raise LockNotAcquired.new(key)
38
38
  if block then
39
39
  begin
40
- result = block.call
40
+ result = (block.arity == 1) ? block.call(self) : block.call
41
41
  ensure
42
42
  release_lock
43
43
  end
@@ -1,5 +1,5 @@
1
1
  class Redis
2
2
  class Lock
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
5
5
  end
@@ -20,10 +20,32 @@ describe Redis::Lock, redis: true do
20
20
  hers.should_not be_locked
21
21
  end
22
22
 
23
- it 'returns the return value of the block' do
24
- hers.lock do
25
- 1
26
- end.should eql(1)
23
+ context "when using blocks" do
24
+
25
+ it 'returns the return value of the block' do
26
+ hers.lock do
27
+ 1
28
+ end.should eql(1)
29
+ end
30
+
31
+ it "returns the return value of the lambda" do
32
+ action = ->() { 1 }
33
+ hers.lock( &action ).should eq(1)
34
+ end
35
+
36
+ it "passes the lock into a supplied block" do
37
+ hers.lock do |lock|
38
+ lock.should be_an_instance_of(Redis::Lock)
39
+ end
40
+ end
41
+
42
+ it "passes the lock into a supplied lambda" do
43
+ action = ->(lock) do
44
+ lock.should be_an_instance_of(Redis::Lock)
45
+ end
46
+ hers.lock( &action )
47
+ end
48
+
27
49
  end
28
50
 
29
51
  it "can prevent other use of a lock" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mlanett-redis-lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-03 00:00:00.000000000 Z
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis