Sha256: b032806ba3ea150929db22b0b23433e6846a339ad8d47ae4b6e08b09bd556f6f

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module FreshConnection
  class SlaveConnection
    class << self
      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_models=(model_names)
        @ignore_models = model_names
      end

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

      def ignore_configure_connection?
        !!@ignore_configure_connection
      end

      def ignore_configure_connection=(flag)
        @ignore_configure_connection = flag
      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.4 lib/fresh_connection/slave_connection.rb