Sha256: 7b5472d7062bb2b5b973b5a1280065681fe80820e8f58c07808bc4d45324565b

Contents?: true

Size: 1.08 KB

Versions: 14

Compression:

Stored size: 1.08 KB

Contents

# This is a simple proxy class used as a default connection on sharded models
#
# The idea is to proxy all utility method calls to a real connection (set by
# the +set_real_connection+ method when we switch shards) and fail on real
# database querying calls forcing users to switch shard connections.
#
module DbCharmer
  class StubConnection
    def initialize(real_conn = nil)
      @real_conn = real_conn
    end

    def set_real_connection(real_conn)
      @real_conn = real_conn
    end

    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_conn
        raise  ActiveRecord::ConnectionNotEstablished, "No real connection to proxy this method to!"
      end

      # Proxy the call to our real connection target
      @real_conn.__send__(meth, *args, &block)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
db-charmer-1.6.16 lib/db_charmer/stub_connection.rb
db-charmer-1.6.15 lib/db_charmer/stub_connection.rb
db-charmer-1.6.14 lib/db_charmer/stub_connection.rb
db-charmer-1.6.13 lib/db_charmer/stub_connection.rb
db-charmer-1.6.12 lib/db_charmer/stub_connection.rb
db-charmer-1.6.11 lib/db_charmer/stub_connection.rb
db-charmer-1.6.10 lib/db_charmer/stub_connection.rb
db-charmer-1.6.9 lib/db_charmer/stub_connection.rb
db-charmer-1.6.8 lib/db_charmer/stub_connection.rb
db-charmer-1.6.7 lib/db_charmer/stub_connection.rb
db-charmer-1.6.6 lib/db_charmer/stub_connection.rb
db-charmer-1.6.5 lib/db_charmer/stub_connection.rb
db-charmer-1.6.4 lib/db_charmer/stub_connection.rb
db-charmer-1.6.3 lib/db_charmer/stub_connection.rb