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
201 202 203 204 |
# File 'lib/activerecord_autoreplica.rb', line 201 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
224 225 226 |
# File 'lib/activerecord_autoreplica.rb', line 224 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
221 222 223 |
# File 'lib/activerecord_autoreplica.rb', line 221 def respond_to_missing?(method_name) @master_connection.respond_to?(method_name) end |