thread_local_accessor 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +2 -0
- data/CHANGELOG.md +4 -0
- data/README.md +19 -2
- data/lib/thread_local_accessor.rb +10 -11
- data/lib/thread_local_accessor/version.rb +1 -1
- data/test/thread_local_accessor_test.rb +23 -0
- metadata +12 -7
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ThreadLocalAccessor
|
2
2
|
|
3
|
-
Thread local accessor. An idea from [here](http://coderrr.wordpress.com/2008/04/10/lets-stop-polluting-the-threadcurrent-hash/).
|
3
|
+
Thread local accessor. An idea and big part of code from [here](http://coderrr.wordpress.com/2008/04/10/lets-stop-polluting-the-threadcurrent-hash/).
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,24 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
It implement thread-safe class accessor implemented via Thread.current:
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
class ThreadedLib
|
25
|
+
thread_local_accessor :some_setting, :default => :default
|
26
|
+
end
|
27
|
+
|
28
|
+
instance = ThreadedLib.new
|
29
|
+
ThreadedLib.some_setting = 5
|
30
|
+
|
31
|
+
ThreadedLib.some_setting # => 5
|
32
|
+
instance.some_setting # => 5
|
33
|
+
|
34
|
+
Thread.new {
|
35
|
+
ThreadedLib.some_setting # => :default
|
36
|
+
instance.some_setting # => :default
|
37
|
+
end
|
38
|
+
```
|
22
39
|
|
23
40
|
## Status
|
24
41
|
|
@@ -3,22 +3,21 @@ require "thread_local_accessor/version"
|
|
3
3
|
# An idea from http://coderrr.wordpress.com/2008/04/10/lets-stop-polluting-the-threadcurrent-hash/
|
4
4
|
class Class
|
5
5
|
def thread_local_accessor name, options = {}
|
6
|
-
m = Module.new
|
7
|
-
|
8
|
-
|
9
|
-
k =
|
6
|
+
m = Module.new do
|
7
|
+
define_method "#{name}=" do |value|
|
8
|
+
k = (Class === self ? self : self.class).object_id.to_s + "_" + name.to_s
|
9
|
+
Thread.current[k] = value
|
10
|
+
end
|
11
|
+
|
12
|
+
define_method name do
|
13
|
+
k = (Class === self ? self : self.class).object_id.to_s + "_" + name.to_s
|
10
14
|
if Thread.current.key?(k)
|
11
15
|
Thread.current[k]
|
12
16
|
else
|
13
|
-
|
17
|
+
options[:default]
|
14
18
|
end
|
15
19
|
end
|
16
|
-
|
17
|
-
def #{name}=(val)
|
18
|
-
k = ((Class === self ? self : self.class).object_id.to_s + '_#{name}').to_sym
|
19
|
-
Thread.current[k] = val
|
20
|
-
end
|
21
|
-
}
|
20
|
+
end
|
22
21
|
|
23
22
|
class_eval do
|
24
23
|
include m
|
@@ -4,6 +4,7 @@ require 'thread_local_accessor'
|
|
4
4
|
# http://coderrr.wordpress.com/2008/04/10/lets-stop-polluting-the-threadcurrent-hash/
|
5
5
|
class ThreadedLib
|
6
6
|
thread_local_accessor :some_setting, :default => :default
|
7
|
+
thread_local_accessor :some_proc, :default => Proc.new{|x| x*x}
|
7
8
|
end
|
8
9
|
|
9
10
|
class TestThreadedClassAttrAccessor < Test::Unit::TestCase
|
@@ -26,4 +27,26 @@ class TestThreadedClassAttrAccessor < Test::Unit::TestCase
|
|
26
27
|
|
27
28
|
assert_equal 5, ThreadedLib.some_setting
|
28
29
|
end
|
30
|
+
|
31
|
+
def test_with_procs
|
32
|
+
instance = ThreadedLib.new
|
33
|
+
|
34
|
+
ThreadedLib.some_proc = Proc.new{|x| x - 1}
|
35
|
+
|
36
|
+
assert_equal 9, ThreadedLib.some_proc.call(10)
|
37
|
+
assert_equal 9, instance.some_proc.call(10)
|
38
|
+
|
39
|
+
Thread.new {
|
40
|
+
instance.some_proc = Proc.new{|x| x + 1}
|
41
|
+
|
42
|
+
assert_equal 11, ThreadedLib.some_proc.call(10)
|
43
|
+
assert_equal 11, instance.some_proc.call(10)
|
44
|
+
}.join
|
45
|
+
|
46
|
+
Thread.new {
|
47
|
+
assert_equal 100, ThreadedLib.some_proc.call(10)
|
48
|
+
}.join
|
49
|
+
|
50
|
+
assert_equal 9, ThreadedLib.some_proc.call(10)
|
51
|
+
end
|
29
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thread_local_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
description: Thread Local Accessor
|
26
31
|
email:
|
27
32
|
- vakhov@gmail.com
|
@@ -54,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
59
|
version: '0'
|
55
60
|
segments:
|
56
61
|
- 0
|
57
|
-
hash:
|
62
|
+
hash: 272763085
|
58
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
64
|
none: false
|
60
65
|
requirements:
|
@@ -63,10 +68,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
68
|
version: '0'
|
64
69
|
segments:
|
65
70
|
- 0
|
66
|
-
hash:
|
71
|
+
hash: 272763085
|
67
72
|
requirements: []
|
68
73
|
rubyforge_project:
|
69
|
-
rubygems_version: 1.8.
|
74
|
+
rubygems_version: 1.8.24
|
70
75
|
signing_key:
|
71
76
|
specification_version: 3
|
72
77
|
summary: Thread Safe Local Accessor
|