pax_global_header00006660000000000000000000000064137467317330014530gustar00rootroot0000000000000052 comment=60eee17b9a5694a9609da34768115cec68afd994 ruby-fiber-local-1.0.0/000077500000000000000000000000001374673173300146645ustar00rootroot00000000000000ruby-fiber-local-1.0.0/.editorconfig000066400000000000000000000000641374673173300173410ustar00rootroot00000000000000root = true [*] indent_style = tab indent_size = 2 ruby-fiber-local-1.0.0/.github/000077500000000000000000000000001374673173300162245ustar00rootroot00000000000000ruby-fiber-local-1.0.0/.github/workflows/000077500000000000000000000000001374673173300202615ustar00rootroot00000000000000ruby-fiber-local-1.0.0/.github/workflows/development.yml000066400000000000000000000015541374673173300233330ustar00rootroot00000000000000name: Development on: [push, pull_request] jobs: test: runs-on: ${{matrix.os}}-latest continue-on-error: ${{matrix.experimental}} strategy: matrix: os: - ubuntu - macos ruby: - 2.5 - 2.6 - 2.7 experimental: [false] env: [""] include: - os: ubuntu ruby: truffleruby experimental: true - os: ubuntu ruby: jruby experimental: true - os: ubuntu ruby: head experimental: true steps: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@master with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run tests timeout-minutes: 5 run: ${{matrix.env}} bundle exec rspec ruby-fiber-local-1.0.0/.gitignore000066400000000000000000000001761374673173300166600ustar00rootroot00000000000000/.bundle/ /.yardoc /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ # rspec failure tracking .rspec_status /gems.locked ruby-fiber-local-1.0.0/.rspec000066400000000000000000000000551374673173300160010ustar00rootroot00000000000000--format documentation --require spec_helper ruby-fiber-local-1.0.0/README.md000066400000000000000000000052121374673173300161430ustar00rootroot00000000000000# Fiber::Local A module to simplify fiber-local state. [![Development Status](https://github.com/socketry/fiber-local/workflows/Development/badge.svg)](https://github.com/socketry/fiber-local/actions?workflow=Development) ## Features - Easily access fiber-local state from a fiber. - Default to shared thread-local state. ## Installation ``` bash $ bundle add fiber-local ``` ## Usage In your own class, e.g. `Logger`: ``` ruby class Logger extend Fiber::Local def initialize @buffer = [] end def log(*arguments) @buffer << arguments end end ``` Now, instead of instantiating your cache `LOGGER = Logger.new`, use `Logger.instance`. It will return a thread-local instance. ``` ruby Thread.new do Logger.instance # => # end Thread.new do Logger.instance # => # end ``` In cases where you have job per fiber or request per fiber, you might want to collect all log output for a specific fiber, you can do the following: ``` ruby Logger.instance # => # Fiber.new do Logger.instance = Logger.new # => # end ``` ## Contributing We welcome contributions to this project. 1. Fork it. 2. Create your feature branch (`git checkout -b my-new-feature`). 3. Commit your changes (`git commit -am 'Add some feature'`). 4. Push to the branch (`git push origin my-new-feature`). 5. Create new Pull Request. ## See Also - [thread-local](https://github.com/socketry/thread-local) — Strictly thread-local variables. ## License Released under the MIT license. Copyright, 2020, by [Samuel G. D. Williams](https://www.codeotaku.com). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ruby-fiber-local-1.0.0/fiber-local.gemspec000066400000000000000000000011571374673173300204140ustar00rootroot00000000000000# frozen_string_literal: true require_relative "lib/fiber/local/version" Gem::Specification.new do |spec| spec.name = "fiber-local" spec.version = Fiber::Local::VERSION spec.summary = "Provides a class-level mixin to make fiber local state easy." spec.authors = ["Samuel Williams"] spec.license = "MIT" spec.homepage = "https://github.com/socketry/fiber-local" spec.files = Dir.glob('{lib}/**/*', File::FNM_DOTMATCH, base: __dir__) spec.required_ruby_version = ">= 2.5.0" spec.add_development_dependency "bundler" spec.add_development_dependency "covered" spec.add_development_dependency "rspec" end ruby-fiber-local-1.0.0/gems.rb000066400000000000000000000002241374673173300161420ustar00rootroot00000000000000source "https://rubygems.org" gemspec group :maintenance, optional: true do gem "bake-bundler" gem "bake-modernize" gem "utopia-project" end ruby-fiber-local-1.0.0/lib/000077500000000000000000000000001374673173300154325ustar00rootroot00000000000000ruby-fiber-local-1.0.0/lib/fiber/000077500000000000000000000000001374673173300165215ustar00rootroot00000000000000ruby-fiber-local-1.0.0/lib/fiber/local.rb000066400000000000000000000040231374673173300201370ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2020, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require_relative "local/version" class Fiber module Local # Instantiate a new thread-local object. # By default, invokes {new} to generate the instance. # @returns [Object] def local self.new end # Get the current thread-local instance. Create it if required. # @returns [Object] The thread-local instance. def instance thread = Thread.current name = self.name if instance = thread[self.name] return instance end unless instance = thread.thread_variable_get(name) if instance = self.local thread.thread_variable_set(name, instance) end end thread[self.name] = instance return instance end # Assigns to the fiber-local instance. # @parameter instance [Object] The object that will become the thread-local instance. def instance= instance thread = Thread.current thread[self.name] = instance end end end ruby-fiber-local-1.0.0/lib/fiber/local/000077500000000000000000000000001374673173300176135ustar00rootroot00000000000000ruby-fiber-local-1.0.0/lib/fiber/local/version.rb000066400000000000000000000023031374673173300216230ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2020, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. class Fiber module Local VERSION = "1.0.0" end end ruby-fiber-local-1.0.0/spec/000077500000000000000000000000001374673173300156165ustar00rootroot00000000000000ruby-fiber-local-1.0.0/spec/fiber/000077500000000000000000000000001374673173300167055ustar00rootroot00000000000000ruby-fiber-local-1.0.0/spec/fiber/local_spec.rb000066400000000000000000000040731374673173300213420ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2020, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'fiber/local' class MyThing extend Fiber::Local end RSpec.describe Fiber::Local do it "has a version number" do expect(Fiber::Local::VERSION).not_to be nil end describe '#instance' do it "creates unique instances in different threads" do instance1 = Thread.new do MyThing.instance end.value instance2 = Thread.new do MyThing.instance end.value expect(instance1).to_not be instance2 end it "returns same instance in same thread" do expect(MyThing.instance).to be MyThing.instance end end describe '#instance=' do let(:object) {Object.new} it "can assign an object to the fiber local instance" do MyThing.instance = object expect(MyThing.instance).to be object end it "has fiber local scope" do Fiber.new do MyThing.instance = object expect(MyThing.instance).to be object end expect(MyThing.instance).to_not be object end end end ruby-fiber-local-1.0.0/spec/spec_helper.rb000066400000000000000000000027671374673173300204500ustar00rootroot00000000000000# frozen_string_literal: true # Copyright, 2020, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'bundler/setup' require 'covered/rspec' RSpec.configure do |config| # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = ".rspec_status" # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! config.expect_with :rspec do |c| c.syntax = :expect end end