Sha256: 2340eba4a3b8c391f31f4b0010ae4519e1fe8431378e00a90be2c714e6167d2d

Contents?: true

Size: 1.87 KB

Versions: 15

Compression:

Stored size: 1.87 KB

Contents

module DbCharmer
  module MultiDbProxy
    # Simple proxy class that switches connections and then proxies all the calls
    # This class is used to implement chained on_db calls
    class OnDbProxy < BlankSlate
      def initialize(proxy_target, slave)
        @proxy_target = proxy_target
        @slave = slave
      end

    private

      def method_missing(meth, *args, &block)
        # Switch connection and proxy the method call
        @proxy_target.on_db(@slave) do |proxy_target|
          res = proxy_target.__send__(meth, *args, &block)

          # If result is a scope/association, return a new proxy for it, otherwise return the result itself
          (res.proxy?) ? OnDbProxy.new(res, @slave) : res
        end
      end
    end

    module ClassMethods
      def on_db(con, proxy_target = nil)
        proxy_target ||= self

        # Chain call
        return OnDbProxy.new(proxy_target, con) unless block_given?

        # Block call
        begin
          self.db_charmer_connection_level += 1
          old_proxy = db_charmer_connection_proxy
          switch_connection_to(con, DbCharmer.connections_should_exist?)
          yield(proxy_target)
        ensure
          switch_connection_to(old_proxy)
          self.db_charmer_connection_level -= 1
        end
      end
    end

    module InstanceMethods
      def on_db(con, proxy_target = nil, &block)
        proxy_target ||= self
        self.class.on_db(con, proxy_target, &block)
      end
    end

    module MasterSlaveClassMethods
      def on_slave(con = nil, proxy_target = nil, &block)
        con ||= db_charmer_random_slave
        raise ArgumentError, "No slaves found in the class and no slave connection given" unless con
        on_db(con, proxy_target, &block)
      end

      def on_master(proxy_target = nil, &block)
        on_db(db_charmer_default_connection, proxy_target, &block)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
db-charmer-1.6.19 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.18 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.17 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.16 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.15 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.14 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.13 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.12 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.11 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.10 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.9 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.8 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.7 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.6 lib/db_charmer/multi_db_proxy.rb
db-charmer-1.6.5 lib/db_charmer/multi_db_proxy.rb