Sha256: 5b30b9a569aa35c0dd4ff4982ebf00a7624dbb7b9d0323631262d2fe8af25427

Contents?: true

Size: 870 Bytes

Versions: 8

Compression:

Stored size: 870 Bytes

Contents

module Octoshark
  module CurrentConnection
    def current_connection
      Thread.current[identifier] || raise(Octoshark::Error::NoCurrentConnection, "No current connection")
    end

    def current_connection?
      !Thread.current[identifier].nil?
    end

    def current_or_default_connection
      Thread.current[identifier] || ActiveRecord::Base.connection_pool.connection
    end

    def without_connection(&block)
      change_connection_reference(nil) do
        yield
      end
    end

    def identifier
      @identifier ||= "octoshark_#{object_id}"
    end

    private
    def change_connection_reference(connection, &block)
      previous_connection = Thread.current[identifier]
      Thread.current[identifier] = connection

      begin
        yield
      ensure
        Thread.current[identifier] = previous_connection
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
octoshark-0.6.0 lib/octoshark/current_connection.rb
octoshark-0.5.1 lib/octoshark/current_connection.rb
octoshark-0.5.0 lib/octoshark/current_connection.rb
octoshark-0.4.0 lib/octoshark/current_connection.rb
octoshark-0.3.0 lib/octoshark/current_connection.rb
octoshark-0.2.2 lib/octoshark/current_connection.rb
octoshark-0.2.1 lib/octoshark/current_connection.rb
octoshark-0.2.0 lib/octoshark/current_connection.rb