class ActiveRecord::ShardFor::ReplicationMapping

Public Class Methods

new(mapping) click to toggle source
# File lib/activerecord/shard_for/replication_mapping.rb, line 4
def initialize(mapping)
  @mapping = mapping
  @lock = Mutex.new
end

Public Instance Methods

switch(from, role_name) { |target_shard_model| ... } click to toggle source

@param [Class] A shard model having connection to specific shard @param [Symbol] A role name of target cluster. @return [Class, Object] if block given then yielded result else

target shard model.
# File lib/activerecord/shard_for/replication_mapping.rb, line 13
def switch(from, role_name)
  @lock.synchronize { constantize! unless constantized? }

  model = @mapping.fetch(role_name)
  target_shard_model = model.shard_repository.fetch_by_key(from.assigned_key)

  if block_given?
    target_shard_model.connection_pool.with_connection { yield target_shard_model }
  else
    target_shard_model
  end
end

Private Instance Methods

constantize!() click to toggle source
# File lib/activerecord/shard_for/replication_mapping.rb, line 28
def constantize!
  @mapping = Hash[@mapping.map { |k, name| [k, name.to_s.constantize] }]
end
constantized?() click to toggle source
# File lib/activerecord/shard_for/replication_mapping.rb, line 32
def constantized?
  @mapping.values.first.is_a? Class
end