Sha256: b840fee346700603a3e28fa60251762bbc05ac91cf9a38a03786c1012eec5d51

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require "active_record"
require "active_support/concern"
require "active_support/core_ext/module/aliasing"

module Replicat
  module Replicable
    extend ActiveSupport::Concern

    included do
      class << self
        def proxy
          @proxy ||= Proxy.new(self)
        end

        alias_method_chain :connection, :proxy

        attr_accessor :connection_name
      end
    end

    module ClassMethods
      def connection_with_proxy
        if has_any_replication?
          proxy
        else
          connection_without_proxy
        end
      end

      def has_any_replication?
        has_configuration? && replications.present?
      end

      def has_configuration?
        !!configuration
      end

      def configuration
        connection_name && configurations[connection_name]
      end

      def replications
        configuration["replications"]
      end

      def using(connection_name)
        proxy.current_connection_name = connection_name
        yield
      ensure
        proxy.current_connection_name = nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
replicat-0.0.1 lib/replicat/replicable.rb