Class: AutoReplica::Adapter
- Inherits:
-
Object
- Object
- AutoReplica::Adapter
- Defined in:
- lib/activerecord_autoreplica.rb
Overview
Acts as a wrapping proxy that replaces an ActiveRecord Adapter object. This is the "connection adapter" object that ActiveRecord uses internally to run SQL queries against. We let it dispatch all SELECT queries to the read replica and all other queries to the master database.
To achieve this, we make a delegator proxy that sends all methods prefixed with "select_" to the read connection, and all the others to the master connection.
Instance Method Summary collapse
-
#initialize(master_connection_adapter, replica_connection_adapter) ⇒ Adapter
constructor
a proxy for ActiveRecord::ConnectionAdapters::AbstractAdapter.
- #method_missing(method_name, *args, &blk) ⇒ Object
-
#respond_to_missing?(method_name) ⇒ Boolean
The duo for method proxying without delegate.
Constructor Details
#initialize(master_connection_adapter, replica_connection_adapter) ⇒ Adapter
a proxy for ActiveRecord::ConnectionAdapters::AbstractAdapter
180 181 182 183 |
# File 'lib/activerecord_autoreplica.rb', line 180 def initialize(master_connection_adapter, replica_connection_adapter) @master_connection = master_connection_adapter @read_connection = replica_connection_adapter end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &blk) ⇒ Object
203 204 205 |
# File 'lib/activerecord_autoreplica.rb', line 203 def method_missing(method_name, *args, &blk) @master_connection.public_send(method_name, *args, &blk) end |
Instance Method Details
#respond_to_missing?(method_name) ⇒ Boolean
The duo for method proxying without delegate
200 201 202 |
# File 'lib/activerecord_autoreplica.rb', line 200 def respond_to_missing?(method_name) @master_connection.respond_to?(method_name) end |