class DbCharmer::Sharding::StubConnection

Attributes

sharded_connection[RW]

Public Class Methods

new(sharded_connection) click to toggle source
# File lib/db_charmer/sharding/stub_connection.rb, line 12
def initialize(sharded_connection)
  @sharded_connection = sharded_connection
  @real_conn = nil
end

Public Instance Methods

db_charmer_connection_name() click to toggle source
# File lib/db_charmer/sharding/stub_connection.rb, line 21
def db_charmer_connection_name
  "StubConnection"
end
method_missing(meth, *args, &block) click to toggle source
# File lib/db_charmer/sharding/stub_connection.rb, line 46
def method_missing(meth, *args, &block)
  # Fail on database statements
  if ::ActiveRecord::ConnectionAdapters::DatabaseStatements.instance_methods.member?(meth.to_s)
    raise ::ActiveRecord::ConnectionNotEstablished, "You have to switch connection on your model before using it!"
  end

  # Fail if no connection has been established yet
  unless real_connection
    raise ::ActiveRecord::ConnectionNotEstablished, "No real connection to proxy this method to!"
  end

  if real_connection.kind_of?(DbCharmer::Sharding::StubConnection)
    raise ::ActiveRecord::ConnectionNotEstablished, "You have to switch connection on your model before using it!"
  end

  # Proxy the call to our real connection target
  real_connection.__send__(meth, *args, &block)
end
real_connection() click to toggle source
# File lib/db_charmer/sharding/stub_connection.rb, line 25
def real_connection
  # Return memoized real connection
  return @real_conn if @real_conn

  # If sharded connection supports shards enumeration, get the first shard
  conn = sharded_connection.shard_connections.try(:first)

  # If we do not have real connection yet, try to use the default one (if it is supported by the sharder)
  conn ||= sharded_connection.sharder.shard_for_key(:default) if sharded_connection.support_default_shard?

  # Get connection proxy for our real connection
  return nil unless conn
  @real_conn = ::ActiveRecord::Base.coerce_to_connection_proxy(conn, DbCharmer.connections_should_exist?)
end
respond_to?(method_name, include_all = false) click to toggle source
Calls superclass method
# File lib/db_charmer/sharding/stub_connection.rb, line 40
def respond_to?(method_name, include_all = false)
  return true if super
  return false if real_connection.object_id == self.object_id
  real_connection.respond_to?(method_name, include_all)
end
set_real_connection(real_conn) click to toggle source
# File lib/db_charmer/sharding/stub_connection.rb, line 17
def set_real_connection(real_conn)
  @real_conn = real_conn
end