thread_variable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thread_variable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 J. Andrew Marshall <http://johnandrewmarshall.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # ThreadVariable
2
+
3
+ [![Build Status](https://secure.travis-ci.org/amarshall/thread_variable.png)](http://travis-ci.org/amarshall/thread_variable)
4
+
5
+ Makes it easy to create and use thread-local variables. This encapsulates two patterns:
6
+
7
+ - The `Thread.current` hash is not namespaced, which can make name collision between different libraries or parts of an app very possible. ThreadVariable gets around this by namespacing variables under the current class/module name.
8
+ - Accessing and setting `Thread.current` is not so pretty to look at, so ThreadVariable creates a getter and setter for each thread variable.
9
+
10
+ ## Installation
11
+
12
+ Install as your normally would, nothing special here. Only compatible with Ruby 1.9+.
13
+
14
+ ## Usage
15
+
16
+ Simply `extend ThreadVariable` and call `thread_variable` in classes you want thread variables in:
17
+
18
+ class C
19
+ extend ThreadVariable
20
+ thread_variable :foo
21
+ end
22
+
23
+ C.foo = 'bar'
24
+
25
+ Thread.new do
26
+ C.foo = 'baz'
27
+ C.foo #=> "baz"
28
+ end.join
29
+
30
+ C.foo #=> "bar"
31
+
32
+ If you don’t want to `extend ThreadLocal` everywhere you can instead do
33
+
34
+ require 'thread_variable/core_ext'
35
+
36
+ which will `include ThreadVariable` in both `Class` & `Module`.
37
+
38
+ For more, just read the source. Seriously, go read it—it’s less than 20 lines and will only take a minute.
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
47
+
48
+ ## Credits & License
49
+
50
+ Copyright © 2012 J. Andrew Marshall. All rights reserved.
51
+ License is available in the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Run specs'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ require 'thread_variable'
2
+
3
+ class Class
4
+ include ThreadVariable
5
+ end
6
+
7
+ class Module
8
+ include ThreadVariable
9
+ end
@@ -0,0 +1,3 @@
1
+ module ThreadVariable
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'thread_variable/version'
2
+
3
+ module ThreadVariable
4
+ def thread_variable *names
5
+ names.each do |name|
6
+ define_singleton_method :"#{name}" do
7
+ namespace = (self.name ? self.name : self.object_id.to_s).to_sym
8
+ (Thread.current[namespace] ||= {})[name]
9
+ end
10
+
11
+ define_singleton_method :"#{name}=" do |val|
12
+ namespace = (self.name ? self.name : self.object_id.to_s).to_sym
13
+ (Thread.current[namespace] ||= {})[name] = val
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ require 'thread_variable'
2
+
3
+ RSpec.configure do |config|
4
+ config.color_enabled = true
5
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe ThreadVariable do
4
+ let(:klass) do
5
+ Class.new do
6
+ extend ThreadVariable
7
+ thread_variable :my_var
8
+ end
9
+ end
10
+
11
+ before do
12
+ # Normally I wouldn't do this, but threads are special
13
+ klass.my_var.should be_nil
14
+ end
15
+
16
+ it "can set and retrieve a thread variable" do
17
+ klass_var = klass.my_var = double
18
+ klass.my_var.should == klass_var
19
+ end
20
+
21
+ it "is local to a thread" do
22
+ klass_var = klass.my_var = double
23
+ Thread.new do
24
+ klass.my_var.should be_nil
25
+ end.join
26
+ klass.my_var.should == klass_var
27
+ end
28
+
29
+ it "namespaces thread variables for anonymous classes" do
30
+ other_klass = Class.new do
31
+ extend ThreadVariable
32
+ thread_variable :my_var
33
+ end
34
+
35
+ klass_var = klass.my_var = double
36
+ other_var = other_klass.my_var = double
37
+
38
+ klass.my_var.should == klass_var
39
+ other_klass.my_var.should == other_var
40
+ end
41
+
42
+ it "namespaces thread variables for named classes" do
43
+ stub_const 'C', klass
44
+ stub_const 'D', (Class.new do
45
+ extend ThreadVariable
46
+ thread_variable :my_var
47
+ end)
48
+
49
+ c_var = C.my_var = double
50
+ d_var = D.my_var = double
51
+
52
+ C.my_var.should == c_var
53
+ D.my_var.should == d_var
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thread_variable/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'thread_variable'
8
+ gem.version = ThreadVariable::VERSION
9
+ gem.authors = ['Andrew Marshall']
10
+ gem.email = ['andrew@johnandrewmarshall.com']
11
+ gem.description = %q{Makes it easy to create and use thread-local variables.}
12
+ gem.summary = %q{Makes it easy to create and use thread-local variables.}
13
+ gem.homepage = 'http://johnandrewmarshall.com/projects/thread_variable'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thread_variable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Marshall
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Makes it easy to create and use thread-local variables.
47
+ email:
48
+ - andrew@johnandrewmarshall.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .travis.yml
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/thread_variable.rb
60
+ - lib/thread_variable/core_ext.rb
61
+ - lib/thread_variable/version.rb
62
+ - spec/spec_helper.rb
63
+ - spec/thread_variable_spec.rb
64
+ - thread_variable.gemspec
65
+ homepage: http://johnandrewmarshall.com/projects/thread_variable
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Makes it easy to create and use thread-local variables.
89
+ test_files:
90
+ - spec/spec_helper.rb
91
+ - spec/thread_variable_spec.rb
92
+ has_rdoc: