scrolls 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/scrolls/atomic.rb +47 -46
- data/lib/scrolls/log.rb +4 -3
- data/lib/scrolls/version.rb +1 -1
- data/test/test_atomic.rb +5 -5
- metadata +3 -3
data/README.md
CHANGED
data/lib/scrolls/atomic.rb
CHANGED
@@ -1,58 +1,59 @@
|
|
1
|
-
# The result of issues with an update I made to Scrolls. After talking with
|
2
|
-
# Fabio Kung about a fix I started work on an atomic object, but he added some
|
3
|
-
# fixes to #context without it and then used Headius' atomic gem.
|
4
|
-
#
|
5
|
-
# The code below is the start and cleanup of my atomic object. It's slim on
|
6
|
-
# details and eventually cleaned up around inspiration from Headius' code.
|
7
|
-
#
|
8
|
-
# LICENSE: Apache 2.0
|
9
|
-
#
|
10
|
-
# See Headius' atomic gem here:
|
11
|
-
# https://github.com/headius/ruby-atomic
|
12
|
-
|
13
1
|
require 'thread'
|
14
2
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
3
|
+
module Scrolls
|
4
|
+
# The result of issues with an update I made to Scrolls. After talking with
|
5
|
+
# Fabio Kung about a fix I started work on an atomic object, but he added some
|
6
|
+
# fixes to #context without it and then used Headius' atomic gem.
|
7
|
+
#
|
8
|
+
# The code below is the start and cleanup of my atomic object. It's slim on
|
9
|
+
# details and eventually cleaned up around inspiration from Headius' code.
|
10
|
+
#
|
11
|
+
# LICENSE: Apache 2.0
|
12
|
+
#
|
13
|
+
# See Headius' atomic gem here:
|
14
|
+
# https://github.com/headius/ruby-atomic
|
15
|
+
class AtomicObject
|
16
|
+
def initialize(o)
|
17
|
+
@mtx = Mutex.new
|
18
|
+
@o = o
|
19
|
+
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
def get
|
22
|
+
@mtx.synchronize { @o }
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def set(n)
|
26
|
+
@mtx.synchronize { @o = n }
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
29
|
+
def verify_set(o, n)
|
30
|
+
return false unless @mtx.try_lock
|
31
|
+
begin
|
32
|
+
return false unless @o.equal? o
|
33
|
+
@o = n
|
34
|
+
ensure
|
35
|
+
@mtx.unlock
|
36
|
+
end
|
36
37
|
end
|
37
38
|
end
|
38
|
-
end
|
39
39
|
|
40
|
-
class Atomic < AtomicObject
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
class Atomic < AtomicObject
|
41
|
+
def initialize(v=nil)
|
42
|
+
super(v)
|
43
|
+
end
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
def value
|
46
|
+
self.get
|
47
|
+
end
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
def value=(v)
|
50
|
+
self.set(v)
|
51
|
+
v
|
52
|
+
end
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
def update
|
55
|
+
true until self.verify_set(o = self.get, n = yield(o))
|
56
|
+
n
|
57
|
+
end
|
57
58
|
end
|
58
|
-
end
|
59
|
+
end
|
data/lib/scrolls/log.rb
CHANGED
@@ -68,7 +68,7 @@ module Scrolls
|
|
68
68
|
else
|
69
69
|
start = Time.now
|
70
70
|
res = nil
|
71
|
-
log(logdata.merge(at
|
71
|
+
log(logdata.merge(:at => "start"))
|
72
72
|
begin
|
73
73
|
res = yield
|
74
74
|
rescue StandardError, Timeout::Error => e
|
@@ -82,7 +82,7 @@ module Scrolls
|
|
82
82
|
)
|
83
83
|
raise e
|
84
84
|
end
|
85
|
-
log(logdata.merge(at
|
85
|
+
log(logdata.merge(:at => "finish", :elapsed => calc_time(start, Time.now)))
|
86
86
|
res
|
87
87
|
end
|
88
88
|
end
|
@@ -117,8 +117,9 @@ module Scrolls
|
|
117
117
|
return unless block_given?
|
118
118
|
old = context
|
119
119
|
self.context = old.merge(prefix)
|
120
|
-
yield if block_given?
|
120
|
+
res = yield if block_given?
|
121
121
|
self.context = old
|
122
|
+
res
|
122
123
|
end
|
123
124
|
|
124
125
|
private
|
data/lib/scrolls/version.rb
CHANGED
data/test/test_atomic.rb
CHANGED
@@ -2,22 +2,22 @@ require_relative "test_helper"
|
|
2
2
|
|
3
3
|
class TestAtomic < Test::Unit::TestCase
|
4
4
|
def test_construct
|
5
|
-
atomic = Atomic.new
|
5
|
+
atomic = Scrolls::Atomic.new
|
6
6
|
assert_equal nil, atomic.value
|
7
7
|
|
8
|
-
atomic = Atomic.new(0)
|
8
|
+
atomic = Scrolls::Atomic.new(0)
|
9
9
|
assert_equal 0, atomic.value
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_value
|
13
|
-
atomic = Atomic.new(0)
|
13
|
+
atomic = Scrolls::Atomic.new(0)
|
14
14
|
atomic.value = 1
|
15
15
|
|
16
16
|
assert_equal 1, atomic.value
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_update
|
20
|
-
atomic = Atomic.new(1000)
|
20
|
+
atomic = Scrolls::Atomic.new(1000)
|
21
21
|
res = atomic.update {|v| v + 1}
|
22
22
|
|
23
23
|
assert_equal 1001, atomic.value
|
@@ -26,7 +26,7 @@ class TestAtomic < Test::Unit::TestCase
|
|
26
26
|
|
27
27
|
def test_update_retries
|
28
28
|
tries = 0
|
29
|
-
atomic = Atomic.new(1000)
|
29
|
+
atomic = Scrolls::Atomic.new(1000)
|
30
30
|
atomic.update{|v| tries += 1 ; atomic.value = 1001 ; v + 1}
|
31
31
|
assert_equal 2, tries
|
32
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scrolls
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
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: 2012-
|
12
|
+
date: 2012-09-16 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Logging, easier, more consistent.
|
15
15
|
email:
|
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
54
|
version: '0'
|
55
55
|
requirements: []
|
56
56
|
rubyforge_project:
|
57
|
-
rubygems_version: 1.8.
|
57
|
+
rubygems_version: 1.8.23
|
58
58
|
signing_key:
|
59
59
|
specification_version: 3
|
60
60
|
summary: When do we log? All the time.
|