thread_variable 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +2 -0
- data/README.md +18 -13
- data/lib/thread_variable.rb +2 -2
- data/lib/thread_variable/version.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/thread_variable_spec.rb +8 -8
- metadata +11 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 692102ea3dcb752e0a3ebfaf61d0107beef38597
|
4
|
+
data.tar.gz: 36ec30f4cef1207eb5c3928b3f0799dfbc7106f8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8629c028a167acc2ce821de13a88c9c77eee039c41dbf297fcb5ffc89d7c7db383f9b2a8b962daa42473254e96e0c70e8360e6e88ebb1018aaf4c6cfb29cf7be
|
7
|
+
data.tar.gz: 77551da443ae0cfa13d81a033c50cc95360b8e501c35cb7ccd4150317f2d58ecffc8c631a035f1fb1597d84bdd3be13104efca8f5fc1ba241b302e14db86cd04
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# ThreadVariable
|
2
2
|
|
3
|
-
[![Build Status](https://secure.travis-ci.org/amarshall/thread_variable.png)](http://travis-ci.org/amarshall/thread_variable)
|
3
|
+
[![Build Status](https://secure.travis-ci.org/amarshall/thread_variable.png?branch=master)](http://travis-ci.org/amarshall/thread_variable)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/amarshall/thread_variable.png)](https://codeclimate.com/github/amarshall/thread_variable)
|
4
5
|
|
5
6
|
Makes it easy to create and use thread-local variables. This encapsulates two patterns:
|
6
7
|
|
@@ -15,23 +16,27 @@ Install as your normally would, nothing special here. Only compatible with Ruby
|
|
15
16
|
|
16
17
|
Simply `extend ThreadVariable` and call `thread_variable` in classes you want thread variables in:
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
```ruby
|
20
|
+
class C
|
21
|
+
extend ThreadVariable
|
22
|
+
thread_variable :foo
|
23
|
+
end
|
22
24
|
|
23
|
-
|
25
|
+
C.foo = 'bar'
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
Thread.new do
|
28
|
+
C.foo = 'baz'
|
29
|
+
C.foo #=> "baz"
|
30
|
+
end.join
|
29
31
|
|
30
|
-
|
32
|
+
C.foo #=> "bar"
|
33
|
+
```
|
31
34
|
|
32
|
-
If you don’t want to `extend
|
35
|
+
If you don’t want to `extend ThreadVariable` everywhere you can instead do
|
33
36
|
|
34
|
-
|
37
|
+
```ruby
|
38
|
+
require 'thread_variable/core_ext'
|
39
|
+
```
|
35
40
|
|
36
41
|
which will `include ThreadVariable` in both `Class` & `Module`.
|
37
42
|
|
data/lib/thread_variable.rb
CHANGED
@@ -3,13 +3,13 @@ require 'thread_variable/version'
|
|
3
3
|
module ThreadVariable
|
4
4
|
def thread_variable *names
|
5
5
|
names.each do |name|
|
6
|
+
namespace = (self.name || self.object_id.to_s).to_sym
|
7
|
+
|
6
8
|
define_singleton_method :"#{name}" do
|
7
|
-
namespace = (self.name ? self.name : self.object_id.to_s).to_sym
|
8
9
|
(Thread.current[namespace] ||= {})[name]
|
9
10
|
end
|
10
11
|
|
11
12
|
define_singleton_method :"#{name}=" do |val|
|
12
|
-
namespace = (self.name ? self.name : self.object_id.to_s).to_sym
|
13
13
|
(Thread.current[namespace] ||= {})[name] = val
|
14
14
|
end
|
15
15
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -10,20 +10,20 @@ describe ThreadVariable do
|
|
10
10
|
|
11
11
|
before do
|
12
12
|
# Normally I wouldn't do this, but threads are special
|
13
|
-
klass.my_var.
|
13
|
+
expect(klass.my_var).to be_nil
|
14
14
|
end
|
15
15
|
|
16
16
|
it "can set and retrieve a thread variable" do
|
17
17
|
klass_var = klass.my_var = double
|
18
|
-
klass.my_var.
|
18
|
+
expect(klass.my_var).to eq klass_var
|
19
19
|
end
|
20
20
|
|
21
21
|
it "is local to a thread" do
|
22
22
|
klass_var = klass.my_var = double
|
23
23
|
Thread.new do
|
24
|
-
klass.my_var.
|
24
|
+
expect(klass.my_var).to be_nil
|
25
25
|
end.join
|
26
|
-
klass.my_var.
|
26
|
+
expect(klass.my_var).to eq klass_var
|
27
27
|
end
|
28
28
|
|
29
29
|
it "namespaces thread variables for anonymous classes" do
|
@@ -35,8 +35,8 @@ describe ThreadVariable do
|
|
35
35
|
klass_var = klass.my_var = double
|
36
36
|
other_var = other_klass.my_var = double
|
37
37
|
|
38
|
-
klass.my_var.
|
39
|
-
other_klass.my_var.
|
38
|
+
expect(klass.my_var).to eq klass_var
|
39
|
+
expect(other_klass.my_var).to eq other_var
|
40
40
|
end
|
41
41
|
|
42
42
|
it "namespaces thread variables for named classes" do
|
@@ -49,7 +49,7 @@ describe ThreadVariable do
|
|
49
49
|
c_var = C.my_var = double
|
50
50
|
d_var = D.my_var = double
|
51
51
|
|
52
|
-
C.my_var.
|
53
|
-
D.my_var.
|
52
|
+
expect(C.my_var).to eq c_var
|
53
|
+
expect(D.my_var).to eq d_var
|
54
54
|
end
|
55
55
|
end
|
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thread_variable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Andrew Marshall
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: Makes it easy to create and use thread-local variables.
|
@@ -64,27 +59,26 @@ files:
|
|
64
59
|
- thread_variable.gemspec
|
65
60
|
homepage: http://johnandrewmarshall.com/projects/thread_variable
|
66
61
|
licenses: []
|
62
|
+
metadata: {}
|
67
63
|
post_install_message:
|
68
64
|
rdoc_options: []
|
69
65
|
require_paths:
|
70
66
|
- lib
|
71
67
|
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
68
|
requirements:
|
74
|
-
- -
|
69
|
+
- - '>='
|
75
70
|
- !ruby/object:Gem::Version
|
76
71
|
version: '0'
|
77
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
-
none: false
|
79
73
|
requirements:
|
80
|
-
- -
|
74
|
+
- - '>='
|
81
75
|
- !ruby/object:Gem::Version
|
82
76
|
version: '0'
|
83
77
|
requirements: []
|
84
78
|
rubyforge_project:
|
85
|
-
rubygems_version:
|
79
|
+
rubygems_version: 2.0.3
|
86
80
|
signing_key:
|
87
|
-
specification_version:
|
81
|
+
specification_version: 4
|
88
82
|
summary: Makes it easy to create and use thread-local variables.
|
89
83
|
test_files:
|
90
84
|
- spec/spec_helper.rb
|