Class: ConnectionProxy

Inherits:
Object show all
Defined in:
lib/mongoid/connection_proxy.rb

Overview

Proxy that enables runtime swapping of the MongoDB connection. This is not an ideal-solution, performance-wise, as a quick check shows that method invocation via method_missing is about three times as slow as direct invocation on the object.

What this does allow us to do, however, is very transparently swap out Mongo::Connection instances underneath Mongoid, without having to rewrite large portions of the codebase. In theory, this should result in a relatively painless connection-swapping system.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ConnectionProxy

Returns a new instance of ConnectionProxy.



19
20
21
# File 'lib/mongoid/connection_proxy.rb', line 19

def initialize(*args)
    @connection = Mongo::Connection.from_uri(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



27
28
29
# File 'lib/mongoid/connection_proxy.rb', line 27

def method_missing(*args, &block)
    @connection.send(*args, &block)
end

Class Method Details

.from_uri(*args) ⇒ Object



15
16
17
# File 'lib/mongoid/connection_proxy.rb', line 15

def ConnectionProxy.from_uri(*args)
    new(*args)
end

Instance Method Details

#db(name) ⇒ Object



23
24
25
# File 'lib/mongoid/connection_proxy.rb', line 23

def db(name)
    @database ||= DatabaseProxy.new(@connection, name)
end