Module: ConnectionManager::Replication
- Defined in:
- lib/connection_manager/replication.rb
Instance Method Summary collapse
-
#fetch_replication_method(method_name) ⇒ Object
Get a connection class name from out replication_methods pool.
- #replicant_classes ⇒ Object
-
#replicated(*connections) ⇒ Object
Builds a class method that returns an ActiveRecord::Relation for use with in ActiveRecord method chaining.
-
#replicated? ⇒ Boolean
Is this class replicated.
-
#replication_methods ⇒ Object
Replication methods (replication_method_name, which is the option for the #replication method) and all their associated connections.
Instance Method Details
#fetch_replication_method(method_name) ⇒ Object
Get a connection class name from out replication_methods pool.
52 53 54 55 56 57 |
# File 'lib/connection_manager/replication.rb', line 52 def fetch_replication_method(method_name) available_connections = @replication_methods[method_name] raise ArgumentError, "No connections found for #{method_name}." if available_connections.blank? available_connections.rotate! available_connections[0] end |
#replicant_classes ⇒ Object
14 15 16 |
# File 'lib/connection_manager/replication.rb', line 14 def replicant_classes @replicant_classes ||= HashWithIndifferentAccess.new end |
#replicated(*connections) ⇒ Object
Builds a class method that returns an ActiveRecord::Relation for use with in ActiveRecord method chaining.
EX: class MyClass < ActiveRecord::Base
replicated :my_readonly_db, "FooConnection", :method_name => 'slaves'
end
end
Options:
-
:name - name of class method to call to access replication, default to slaves
-
:readonly - forces all results to readonly
-
:type - the type of replication; :slave or :master, defaults to :slave
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/connection_manager/replication.rb', line 36 def replicated(*connections) @replicated = true = {:name => "slaves"}.merge!(connections.) [:type] ||= :slaves [:build_replicants] = true if ([:build_replicants].blank? && [:type] == :masters) use_database(current_database_name, :table_name => table_name) # make sure the base class has current_database_name set connections = connection.replication_keys([:type]) if connections.blank? set_replications_to_method(connections,[:name]) build_repliciation_class_method() build_replication_association_class() build_query_method_alias_method([:name]) build_repliciation_instance_method([:name]) [:name] end |
#replicated? ⇒ Boolean
Is this class replicated
19 20 21 |
# File 'lib/connection_manager/replication.rb', line 19 def replicated? (@replicated == true) end |
#replication_methods ⇒ Object
Replication methods (replication_method_name, which is the option for the #replication method) and all their associated connections. The key is the replication_method_name and the value is an array of all the replication_classes the replication_method has access to.
EX: replication_methods => [‘Slave1Connection’,Slave2Connection]
10 11 12 |
# File 'lib/connection_manager/replication.rb', line 10 def replication_methods @replication_methods ||= HashWithIndifferentAccess.new end |