Sha256: 4a71c3d59a71257d880c24e6e7dff8faa3592ca2f007752f9637687efd182b2e

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module FreshConnection
  class SlaveConnection
    class << self
      attr_writer :ignore_models, :ignore_configure_connection, :master_clear_connection

      def connection
        slave_connection
      end

      def clear_all_connections!
        if @slave_connections.present?
          @slave_connections.each_value{|c| c && c.disconnect! rescue nil}
        end
        @slave_connections = {}
      end

      def slave_access_in
        Thread.current[:fresh_connection_slave_access] = true
      end

      def slave_access_out
        Thread.current[:fresh_connection_slave_access] = false
      end

      def slave_access?
        Thread.current[:fresh_connection_slave_access] ||= false
      end

      def ignore_model?(model_name)
        (@ignore_models || []).include?(model_name)
      end

      def ignore_configure_connection?
        !!@ignore_configure_connection
      end

      def master_clear_connection?
        @master_clear_connection.nil? || @master_clear_connection
      end

      private

      def slave_connection
        @slave_connections ||= {}
        @slave_connections[current_thread_id] ||= new_connection
      end

      def clear_slave_connection
        @slave_connections[current_thread_id] = nil
      end

      def new_connection
        ActiveRecord::Base.send("#{spec["adapter"]}_connection", spec)
      end

      def spec
        @spec ||= get_spec
      end

      def get_spec
        ret = ActiveRecord::Base.configurations[Rails.env]
        ret.merge(ret["slave"] || {})
      end

      def current_thread_id
        Thread.current.object_id
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fresh_connection-0.0.7 lib/fresh_connection/slave_connection.rb