Class: DbCharmer::ActiveRecord::MultiDbProxy::OnDbProxy

Inherits:
ActiveSupport::BasicObject
Defined in:
lib/db_charmer/active_record/multi_db_proxy.rb

Overview

Simple proxy class that switches connections and then proxies all the calls This class is used to implement chained on_db calls

Instance Method Summary collapse

Constructor Details

#initialize(proxy_target, slave) ⇒ OnDbProxy

Returns a new instance of OnDbProxy.



10
11
12
13
# File 'lib/db_charmer/active_record/multi_db_proxy.rb', line 10

def initialize(proxy_target, slave)
  @proxy_target = proxy_target
  @slave = slave
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



17
18
19
20
21
22
23
24
25
# File 'lib/db_charmer/active_record/multi_db_proxy.rb', line 17

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