ohm-contrib 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/ohm/contrib/locking.rb +53 -0
- data/lib/ohm/contrib.rb +3 -2
- data/ohm-contrib.gemspec +3 -2
- data/test/test_ohm_contrib.rb +7 -1
- metadata +4 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.12
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Ohm
|
2
|
+
# This module is a straight extraction from Ohm. The only difference is
|
3
|
+
# that this allows for a custom sleep value.
|
4
|
+
# In addition, since future ohm versions might drop mutexes, I thought it
|
5
|
+
# might be a good idea to preseve this feature as a drop-in module.
|
6
|
+
module Locking
|
7
|
+
# Public: Lock the object before executing the block, and release it once the block is done.
|
8
|
+
#
|
9
|
+
# Examples:
|
10
|
+
#
|
11
|
+
# post = Order.create(:customer => Customer.create)
|
12
|
+
# post.mutex(0.01) do
|
13
|
+
# # this block is in a mutex!
|
14
|
+
# end
|
15
|
+
def mutex(wait = 0.1)
|
16
|
+
lock!(wait)
|
17
|
+
yield
|
18
|
+
self
|
19
|
+
ensure
|
20
|
+
unlock!
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
# Lock the object so no other instances can modify it.
|
25
|
+
# This method implements the design pattern for locks
|
26
|
+
# described at: http://code.google.com/p/redis/wiki/SetnxCommand
|
27
|
+
#
|
28
|
+
# @see Model#mutex
|
29
|
+
def lock!(wait = 0.1)
|
30
|
+
until db.setnx(key(:_lock), lock_timeout)
|
31
|
+
next unless lock = db.get(key(:_lock))
|
32
|
+
sleep(wait) and next unless lock_expired?(lock)
|
33
|
+
|
34
|
+
break unless lock = db.getset(key(:_lock), lock_timeout)
|
35
|
+
break if lock_expired?(lock)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Release the lock.
|
40
|
+
# @see Model#mutex
|
41
|
+
def unlock!
|
42
|
+
db.del(key(:_lock))
|
43
|
+
end
|
44
|
+
|
45
|
+
def lock_timeout
|
46
|
+
Time.now.to_f + 1
|
47
|
+
end
|
48
|
+
|
49
|
+
def lock_expired? lock
|
50
|
+
lock.to_f < Time.now.to_f
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/ohm/contrib.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Ohm
|
2
2
|
module Contrib
|
3
|
-
VERSION = '0.0.
|
3
|
+
VERSION = '0.0.12'
|
4
4
|
end
|
5
5
|
|
6
6
|
autoload :Boundaries, "ohm/contrib/boundaries"
|
@@ -9,4 +9,5 @@ module Ohm
|
|
9
9
|
autoload :WebValidations, "ohm/contrib/web_validations"
|
10
10
|
autoload :NumberValidations, "ohm/contrib/number_validations"
|
11
11
|
autoload :Typecast, "ohm/contrib/typecast"
|
12
|
-
|
12
|
+
autoload :Locking, "ohm/contrib/locking"
|
13
|
+
end
|
data/ohm-contrib.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ohm-contrib}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.12"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Cyril David"]
|
12
|
-
s.date = %q{2010-05-
|
12
|
+
s.date = %q{2010-05-19}
|
13
13
|
s.description = %q{Highly decoupled drop-in functionality for Ohm models}
|
14
14
|
s.email = %q{cyx.ucron@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"lib/ohm/contrib.rb",
|
27
27
|
"lib/ohm/contrib/boundaries.rb",
|
28
|
+
"lib/ohm/contrib/locking.rb",
|
28
29
|
"lib/ohm/contrib/number_validations.rb",
|
29
30
|
"lib/ohm/contrib/timestamping.rb",
|
30
31
|
"lib/ohm/contrib/to_hash.rb",
|
data/test/test_ohm_contrib.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 12
|
9
|
+
version: 0.0.12
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Cyril David
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-19 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- VERSION
|
96
96
|
- lib/ohm/contrib.rb
|
97
97
|
- lib/ohm/contrib/boundaries.rb
|
98
|
+
- lib/ohm/contrib/locking.rb
|
98
99
|
- lib/ohm/contrib/number_validations.rb
|
99
100
|
- lib/ohm/contrib/timestamping.rb
|
100
101
|
- lib/ohm/contrib/to_hash.rb
|