Sha256: 1c9ec3d63abc4d564dfef598275d1b3236fa468cff1aee6d06e325b22327e31d

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true
require 'fresh_connection/access_control'
require 'fresh_connection/replica_connection_handler'

module FreshConnection
  module Extend
    module ArBase
      def replica_connection_specification_name
        if defined?(@replica_connection_specification_name)
          return @replica_connection_specification_name
        end

        if self == ActiveRecord::Base
          "replica"
        else
          superclass.replica_connection_specification_name
        end
      end

      def replica_connection_specification_name=(spec_name)
        spec_name = spec_name.to_s
        spec_name = "replica" if spec_name.empty?

        @replica_connection_specification_name = spec_name
      end

      def connection
        c = super
        c.model_class = self
        c
      end

      def read_master
        all.read_master
      end

      def with_master(&block)
        all.manage_access(false, &block)
      end

      def establish_fresh_connection(spec_name = "replica")
        self.replica_connection_specification_name = spec_name
        replica_connection_handler.establish_connection(replica_connection_specification_name)
      end

      def replica_connection
        replica_connection_handler.connection(replica_connection_specification_name)
      end

      def clear_all_replica_connections!
        replica_connection_handler.clear_all_connections!
      end

      def master_db_only!
        @_fresh_connection_master_only = true
      end

      def master_db_only?
        @_fresh_connection_master_only ||=
          (self != ActiveRecord::Base && superclass.master_db_only?)
      end

      def replica_connection_put_aside!
        replica_connection_handler.put_aside!
      end

      def replica_connection_recovery?
        replica_connection_handler.recovery?(replica_connection_specification_name)
      end

      private

      def replica_connection_handler
        FreshConnection::ReplicaConnectionHandler.instance
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fresh_connection-3.0.0.rc1 lib/fresh_connection/extend/ar_base.rb