thread-parent 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +10 -0
- data/lib/thread_parent/version.rb +3 -0
- data/lib/thread_parent.rb +24 -0
- data/test/helper.rb +2 -0
- data/test/test_thread_parent.rb +42 -0
- data/thread-parent.gemspec +20 -0
- metadata +74 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mike Evans
|
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,69 @@
|
|
1
|
+
# ThreadParent
|
2
|
+
|
3
|
+
ThreadParent facilitates spawning threads that maintain a reference to the thread that created them.
|
4
|
+
The primary goal is to allow thread local variable lookup through the ancestor chain.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'thread-parent'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install thread-parent
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
You can either create ThreadParent::Thread directly:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'thread-parent'
|
26
|
+
|
27
|
+
Thread.current[:abc] = 'abc'
|
28
|
+
|
29
|
+
ThreadParent::Thread.new do |thread|
|
30
|
+
|
31
|
+
thread.parent == Thread.main #= true
|
32
|
+
|
33
|
+
# Since the thread variable isn't set locally it will be found in its parent.
|
34
|
+
Thread.current[:abc] #= 'abc'
|
35
|
+
|
36
|
+
# Local thread variable assignments work as expected.
|
37
|
+
Thread.current[:def] = 'def'
|
38
|
+
|
39
|
+
ThreadParent::Thread.new do
|
40
|
+
Thread.current[:def] #= 'def'
|
41
|
+
|
42
|
+
# The parent lookup will continue to the parent's parent until a variable is found.
|
43
|
+
Thread.current[:abc] #= 'abc'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
Or include the module to hijack references to Thread:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'thread-parent'
|
52
|
+
include ThreadParent
|
53
|
+
|
54
|
+
Thread.new do
|
55
|
+
# This is really a ThreadParent::Thread
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
## Code Status
|
60
|
+
|
61
|
+
[![Build Status](https://api.travis-ci.org/mje113/thread-parent.png)](http://travis-ci.org/mje113/thread-parent)
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'thread_parent/version'
|
2
|
+
require 'thread'
|
3
|
+
|
4
|
+
module ThreadParent
|
5
|
+
class Thread < Thread
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@_parent = Thread.current
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def parent
|
13
|
+
@_parent
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](key)
|
17
|
+
if key?(key)
|
18
|
+
super
|
19
|
+
else
|
20
|
+
parent[key]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestThreadParent < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
include ThreadParent
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Thread.current[:a] = 'a'
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_can_have_parent
|
12
|
+
thread = Thread.new { 'work' }.join
|
13
|
+
assert_equal Thread.current, thread.parent
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_can_find_thread_variable_in_parent
|
17
|
+
thread = Thread.new { 'work' }.join
|
18
|
+
assert_equal 'a', thread[:a]
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_can_find_thread_variable_in_parents_parent
|
22
|
+
Thread.new {
|
23
|
+
Thread.new {
|
24
|
+
assert_equal 'a', Thread.current[:a]
|
25
|
+
}.join
|
26
|
+
}.join
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_can_override_parents_thread_variable
|
30
|
+
thread = Thread.new {
|
31
|
+
Thread.current[:a] = 'b'
|
32
|
+
}.join
|
33
|
+
|
34
|
+
assert_equal 'b', thread[:a]
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_wont_break_parent_threads_scope
|
38
|
+
Thread.new { Thread.current[:a] = 'b' }.join
|
39
|
+
thread = Thread.new { 'work' }.join
|
40
|
+
assert_equal('a', thread[:a])
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'thread_parent/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'thread-parent'
|
8
|
+
gem.version = ThreadParent::VERSION
|
9
|
+
gem.authors = ['Mike Evans']
|
10
|
+
gem.email = ['mike@urlgonomics.com']
|
11
|
+
gem.description = %q{ThreadParent facilitates spawning threads that maintain a reference to the thread that created them. The primary goal is to allow thread local variable lookup through the ancestor chain.}
|
12
|
+
gem.summary = gem.description
|
13
|
+
gem.homepage = 'https://github.com/mje113/thread-parent'
|
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
|
+
gem.add_dependency 'pry'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thread-parent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mike Evans
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: pry
|
16
|
+
requirement: &70115845155260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70115845155260
|
25
|
+
description: ThreadParent facilitates spawning threads that maintain a reference to
|
26
|
+
the thread that created them. The primary goal is to allow thread local variable
|
27
|
+
lookup through the ancestor chain.
|
28
|
+
email:
|
29
|
+
- mike@urlgonomics.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- .travis.yml
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/thread_parent.rb
|
41
|
+
- lib/thread_parent/version.rb
|
42
|
+
- test/helper.rb
|
43
|
+
- test/test_thread_parent.rb
|
44
|
+
- thread-parent.gemspec
|
45
|
+
homepage: https://github.com/mje113/thread-parent
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.17
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: ThreadParent facilitates spawning threads that maintain a reference to the
|
69
|
+
thread that created them. The primary goal is to allow thread local variable lookup
|
70
|
+
through the ancestor chain.
|
71
|
+
test_files:
|
72
|
+
- test/helper.rb
|
73
|
+
- test/test_thread_parent.rb
|
74
|
+
has_rdoc:
|