thread-parent 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.0.0
3
4
  - 1.9.3
4
5
  - jruby-19mode
5
6
 
data/README.md CHANGED
@@ -19,46 +19,46 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
- You can either create ThreadParent::Thread directly:
22
+ Thread is extended to provide direct access to its 'parent', or the thread where the current
23
+ thread was created. It also provides a way to lookup through its ancestor chain for Thread-local variables.
23
24
 
24
25
  ```ruby
25
- require 'thread-parent'
26
+ require 'thread_parent'
26
27
 
27
28
  Thread.current[:abc] = 'abc'
28
29
 
29
- ThreadParent::Thread.new do |thread|
30
+ Thread.new do |thread|
30
31
 
31
- thread.parent == Thread.main #= true
32
+ thread.parent == Thread.main #=> true
32
33
 
33
- # Since the thread variable isn't set locally it will be found in its parent.
34
- Thread.current[:abc] #= 'abc'
34
+ # Standard local variable lookup behaves as expected.
35
+ thread[:abc] #=> nil
35
36
 
36
- # Local thread variable assignments work as expected.
37
- Thread.current[:def] = 'def'
37
+ # Lookup through the ancestor chain is now supported.
38
+ thread.parents[:abc] #=> 'abc'
38
39
 
39
- ThreadParent::Thread.new do
40
- Thread.current[:def] #= 'def'
40
+ # Local thread variable assignments works as expected.
41
+ thread[:def] = 'def'
42
+ thread[:def] #=> 'def'
43
+ thread.parents[:def] #=> 'def' <- The calling thread is always checked first.
41
44
 
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
- ```
45
+ # Short-hand references to the current thread's parents is also provided.
46
+ Thread.parents[:abc] #=> 'abc'
47
+ Thread.parent == Thread.main #=> true
47
48
 
48
- Or include the module to hijack references to Thread:
49
+ Thread.new do
50
+ Thread.parents[:def] #= 'def'
49
51
 
50
- ```ruby
51
- require 'thread-parent'
52
- include ThreadParent
53
-
54
- Thread.new do
55
- # This is really a ThreadParent::Thread
52
+ # The parent lookup will continue to the parent's parent until a variable is found.
53
+ Thread.parents[:abc] #= 'abc'
54
+ end
56
55
  end
57
56
  ```
58
57
 
59
58
  ## Code Status
60
59
 
61
60
  [![Build Status](https://api.travis-ci.org/mje113/thread-parent.png)](http://travis-ci.org/mje113/thread-parent)
61
+ [![Code Climate](https://codeclimate.com/github/mje113/thread-parent.png)](https://codeclimate.com/github/mje113/thread-parent)
62
62
 
63
63
  ## Contributing
64
64
 
data/lib/thread_parent.rb CHANGED
@@ -2,23 +2,45 @@ require 'thread_parent/version'
2
2
  require 'thread'
3
3
 
4
4
  module ThreadParent
5
- class Thread < Thread
6
5
 
7
- def initialize
8
- @_parent = Thread.current
9
- super
10
- end
6
+ class Parents
11
7
 
12
- def parent
13
- @_parent
8
+ def initialize(child)
9
+ @child = child
14
10
  end
15
11
 
16
12
  def [](key)
17
- if key?(key)
18
- super
13
+ if @child.key?(key)
14
+ @child[key]
19
15
  else
20
- parent[key]
16
+ @child.parent.parents[key]
21
17
  end
22
18
  end
23
19
  end
24
20
  end
21
+
22
+ class Thread
23
+
24
+ alias_method :_initialize, :initialize
25
+
26
+ def initialize(*args, &block)
27
+ @_parent = Thread.current
28
+ _initialize(*args, &block)
29
+ end
30
+
31
+ def parent
32
+ @_parent
33
+ end
34
+
35
+ def parents
36
+ ThreadParent::Parents.new(self)
37
+ end
38
+
39
+ def self.parent
40
+ Thread.current.parent
41
+ end
42
+
43
+ def self.parents
44
+ ThreadParent::Parents.new(Thread.current)
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module ThreadParent
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -2,12 +2,15 @@ require 'helper'
2
2
 
3
3
  class TestThreadParent < MiniTest::Unit::TestCase
4
4
 
5
- include ThreadParent
6
-
7
5
  def setup
8
6
  Thread.current[:a] = 'a'
9
7
  end
10
8
 
9
+ def test_is_a_thread_parent
10
+ thread = Thread.new { 'work' }
11
+ assert_kind_of Thread, thread
12
+ end
13
+
11
14
  def test_can_have_parent
12
15
  thread = Thread.new { 'work' }.join
13
16
  assert_equal Thread.current, thread.parent
@@ -15,13 +18,13 @@ class TestThreadParent < MiniTest::Unit::TestCase
15
18
 
16
19
  def test_can_find_thread_variable_in_parent
17
20
  thread = Thread.new { 'work' }.join
18
- assert_equal 'a', thread[:a]
21
+ assert_equal 'a', thread.parents[:a]
19
22
  end
20
23
 
21
24
  def test_can_find_thread_variable_in_parents_parent
22
25
  Thread.new {
23
26
  Thread.new {
24
- assert_equal 'a', Thread.current[:a]
27
+ assert_equal 'a', Thread.current.parents[:a]
25
28
  }.join
26
29
  }.join
27
30
  end
@@ -31,12 +34,12 @@ class TestThreadParent < MiniTest::Unit::TestCase
31
34
  Thread.current[:a] = 'b'
32
35
  }.join
33
36
 
34
- assert_equal 'b', thread[:a]
37
+ assert_equal 'b', thread.parents[:a]
35
38
  end
36
39
 
37
40
  def test_wont_break_parent_threads_scope
38
41
  Thread.new { Thread.current[:a] = 'b' }.join
39
42
  thread = Thread.new { 'work' }.join
40
- assert_equal('a', thread[:a])
43
+ assert_equal('a', thread.parents[:a])
41
44
  end
42
- end
45
+ end
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
+
20
+ gem.add_development_dependency 'rake'
19
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thread-parent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-09 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-05-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70120867349120 !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: *70120867349120
14
25
  description: ThreadParent facilitates spawning threads that maintain a reference to
15
26
  the thread that created them. The primary goal is to allow thread local variable
16
27
  lookup through the ancestor chain.