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