thread_attr_accessor 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6cee1e0b394fc0e2be3ba4368f5c149f82dfd640
4
+ data.tar.gz: b78986ae387a8f3a45d5f3605eac3dc7f04c80de
5
+ SHA512:
6
+ metadata.gz: b2fc2c4a69a29bfffef454ad01de4450ca80c3c2fdb7b9e97debbe4a459f41e255e126fb6876a887e7665d9f27bc12c4a960449e101c8dcdd8bbb73f5551b33e
7
+ data.tar.gz: 7473479345109300dec056c138edbe947b7e767e65f1089dd675dbcf36e59293f44383ae2536e49a64a34b32bae2c1fb8a08146c4f7ef2e3b972bae953f25576
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thread_attr_accessor.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TN Tecnologia e Ńegócios
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # ThreadAttrAccessor
2
+
3
+ A thread-scoped, module-level `attr_accessor` that allows inheritance.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'thread_attr_accessor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install thread_attr_accessor
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/[my-github-username]/thread_attr_accessor/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "thread_attr_accessor"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # Ruby doesn't provide a standard way to look up a thread's parent
2
+
3
+ class Thread
4
+ module ParentThread
5
+ attr_reader :parent_thread
6
+
7
+ def initialize(*args, **opts, &block)
8
+ @parent_thread = Thread.current
9
+ super
10
+ end
11
+ end
12
+
13
+ prepend ParentThread
14
+
15
+ # For the currently running threads, assume they were created by the main
16
+ # thread - which should be true during the system initialization
17
+ list.each do |thread|
18
+ next if thread == Thread.main
19
+
20
+ if thread.parent_thread.nil?
21
+ thread.instance_eval { @parent_thread = Thread.main }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,103 @@
1
+ require_relative "thread_attr_accessor/version"
2
+ require_relative 'core_ext/thread/parent_thread'
3
+
4
+ # `extend` this module on your class/module to get per-thread class attribute
5
+ # accessors. Example:
6
+ #
7
+ # class MyClass
8
+ # extend ThreadAttrAccessor
9
+ #
10
+ # thread_attr_accessor :setting
11
+ # end
12
+ #
13
+ # MyClass.setting = :original
14
+ #
15
+ # threads = [
16
+ # Thread.new { MyClass.setting = :foo; puts MyClass.setting },
17
+ # Thread.new { MyClass.setting = :bar; puts MyClass.setting },
18
+ # ]
19
+ #
20
+ # threads.each(&:join)
21
+ # MyClass.setting == :original # true
22
+ module ThreadAttrAccessor
23
+ def self.thread_accessor_key(base, name)
24
+ "#{base.name}.#{name}"
25
+ end
26
+
27
+ def self.search_in_ancestor_threads(key)
28
+ ancestor = Thread.current.parent_thread
29
+
30
+ until ancestor.nil? || (ancestor_value = ancestor[key])
31
+ ancestor = ancestor.parent_thread
32
+ end
33
+
34
+ ancestor_value
35
+ end
36
+
37
+ def self.extended(base)
38
+ mod = Module.new
39
+
40
+ unless base.const_defined?(:ThreadAttributeAccessors, false)
41
+ base.const_set(:ThreadAttributeAccessors, mod)
42
+ base.extend(mod)
43
+ end
44
+ end
45
+
46
+ def thread_attr_writer(*names, private: false, **opts)
47
+ mod = const_get(:ThreadAttributeAccessors)
48
+
49
+ names.each do |name|
50
+ thread_key = ThreadAttrAccessor.thread_accessor_key(self, name)
51
+
52
+ mod.send(:define_method, "#{name}=") do |value|
53
+ Thread.current[thread_key] = value
54
+ end
55
+
56
+ if private
57
+ mod.send :private, "#{name}="
58
+ end
59
+ end
60
+ end
61
+
62
+ def thread_attr_reader(*names, default: nil, inherit: false, private: false, **opts)
63
+ if default && inherit
64
+ get_default = ->(thread_key) {
65
+ ThreadAttrAccessor.search_in_ancestor_threads(thread_key) ||
66
+ default.call
67
+ }
68
+ elsif inherit
69
+ get_default = ThreadAttrAccessor.method(:search_in_ancestor_threads)
70
+ elsif default
71
+ get_default = ->(*) { default.call }
72
+ end
73
+
74
+ if get_default
75
+ get_value = ->(thread_key) {
76
+ Thread.current[thread_key] ||= get_default.call(thread_key)
77
+ }
78
+ else
79
+ get_value = ->(thread_key) {
80
+ Thread.current[thread_key]
81
+ }
82
+ end
83
+
84
+ mod = const_get(:ThreadAttributeAccessors)
85
+
86
+ names.each do |name|
87
+ thread_key = ThreadAttrAccessor.thread_accessor_key(self, name)
88
+
89
+ mod.send(:define_method, name) do
90
+ get_value.call(thread_key)
91
+ end
92
+
93
+ if private
94
+ mod.send :private, name
95
+ end
96
+ end
97
+ end
98
+
99
+ def thread_attr_accessor(*names, **opts)
100
+ thread_attr_reader(*names, **opts)
101
+ thread_attr_writer(*names, **opts)
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module ThreadAttrAccessor
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thread_attr_accessor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "thread_attr_accessor"
8
+ spec.version = ThreadAttrAccessor::VERSION
9
+ spec.authors = ["Renato Zannon"]
10
+ spec.email = ["zannon@tn.com.br"]
11
+
12
+ spec.summary = %q{A thread-aware module-level attr_accessor}
13
+ spec.homepage = "https://github.com/tecnologiaenegocios/thread_attr_accessor"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.1.0"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = []
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thread_attr_accessor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Renato Zannon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - zannon@tn.com.br
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/core_ext/thread/parent_thread.rb
58
+ - lib/thread_attr_accessor.rb
59
+ - lib/thread_attr_accessor/version.rb
60
+ - thread_attr_accessor.gemspec
61
+ homepage: https://github.com/tecnologiaenegocios/thread_attr_accessor
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.1.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: A thread-aware module-level attr_accessor
85
+ test_files: []
86
+ has_rdoc: