diff --git a/lib/chewy.rb b/lib/chewy.rb index 8aeb123ab..372eb3879 100644 --- a/lib/chewy.rb +++ b/lib/chewy.rb @@ -62,6 +62,12 @@ module Chewy class << self attr_accessor :adapters + # A thread-local variables accessor + # @return [Hash] + def thread_local_data + Thread.current[:chewy] ||= {} + end + # Derives an index for the passed string identifier if possible. # # @example @@ -86,7 +92,7 @@ def derive_name(index_name) # Main elasticsearch-ruby client instance # def client - Thread.current[:chewy_client] ||= begin + Chewy.thread_local_data[:chewy_client] ||= begin client_configuration = configuration.deep_dup client_configuration.delete(:prefix) # used by Chewy, not relevant to Elasticsearch::Client block = client_configuration[:transport_options].try(:delete, :proc) @@ -138,15 +144,15 @@ def massacre # city3.do_update! # index updated again # def strategy(name = nil, &block) - Thread.current[:chewy_strategy] ||= Chewy::Strategy.new + Chewy.thread_local_data[:chewy_strategy] ||= Chewy::Strategy.new if name if block - Thread.current[:chewy_strategy].wrap name, &block + Chewy.thread_local_data[:chewy_strategy].wrap name, &block else - Thread.current[:chewy_strategy].push name + Chewy.thread_local_data[:chewy_strategy].push name end else - Thread.current[:chewy_strategy] + Chewy.thread_local_data[:chewy_strategy] end end diff --git a/lib/chewy/runtime.rb b/lib/chewy/runtime.rb index 76def12c0..03fc9eeb5 100644 --- a/lib/chewy/runtime.rb +++ b/lib/chewy/runtime.rb @@ -3,7 +3,7 @@ module Chewy module Runtime def self.version - Thread.current[:chewy_runtime_version] ||= Version.new(Chewy.client.info['version']['number']) + Chewy.thread_local_data[:chewy_runtime_version] ||= Version.new(Chewy.client.info['version']['number']) end end end diff --git a/lib/chewy/search/scoping.rb b/lib/chewy/search/scoping.rb index 0596555a2..df0a7681a 100644 --- a/lib/chewy/search/scoping.rb +++ b/lib/chewy/search/scoping.rb @@ -29,7 +29,7 @@ module ClassMethods # # @return [Array] array of scopes def scopes - Thread.current[:chewy_scopes] ||= [] + Chewy.thread_local_data[:chewy_scopes] ||= [] end end diff --git a/spec/chewy_spec.rb b/spec/chewy_spec.rb index cf1c6d037..cfdbfcf8a 100644 --- a/spec/chewy_spec.rb +++ b/spec/chewy_spec.rb @@ -50,13 +50,13 @@ end describe '.client' do - let!(:initial_client) { Thread.current[:chewy_client] } + let!(:initial_client) { Chewy.thread_local_data[:chewy_client] } let(:faraday_block) { proc {} } let(:mock_client) { double(:client) } let(:expected_client_config) { {transport_options: {}} } before do - Thread.current[:chewy_client] = nil + Chewy.thread_local_data[:chewy_client] = nil allow(Chewy).to receive_messages(configuration: {transport_options: {proc: faraday_block}}) allow(::Elasticsearch::Client).to receive(:new).with(expected_client_config) do |*_args, &passed_block| @@ -70,7 +70,7 @@ its(:client) { is_expected.to eq(mock_client) } - after { Thread.current[:chewy_client] = initial_client } + after { Chewy.thread_local_data[:chewy_client] = initial_client } end describe '.create_indices' do