Class: CollectionProxy
Overview
Proxy that enables runtime swapping of a MongoDB collection, as it appears to be cached at several points within Mongoid.
This proxy is generated by the DatabaseProxy when asked for a specific collection.
Class Method Summary collapse
-
.mutex ⇒ Object
Collection-global mutex…
-
.pool ⇒ Object
Accessor for class-level instance variable that holds all the db-collection pairs that we know about, so that we can switch all of them globally.
Instance Method Summary collapse
-
#initialize(db, name, opts) ⇒ CollectionProxy
constructor
Set our default connection and name.
-
#method_missing(*args, &block) ⇒ Object
Proxy methods to the correct collection.
-
#synchronize(&block) ⇒ Object
Convenience method for synchronizing threads.
Constructor Details
#initialize(db, name, opts) ⇒ CollectionProxy
Set our default connection and name.
36 37 38 |
# File 'lib/mongoid/collection_proxy.rb', line 36 def initialize(db, name, opts) @db, @name, @opts = db, name, opts end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
Proxy methods to the correct collection. We do this by trying to find a matching collection in our pool, and if not, we create one using the name and opts initially passed to initialize.
52 53 54 55 56 57 58 |
# File 'lib/mongoid/collection_proxy.rb', line 52 def method_missing(*args, &block) collection = synchronize do pool = (CollectionProxy.pool[@db.target] ||= {}) pool[@name] ||= @db.target.create_collection(@name, @opts) end collection.send(*args, &block) end |
Class Method Details
.mutex ⇒ Object
Collection-global mutex… for great thread safety!
29 30 31 |
# File 'lib/mongoid/collection_proxy.rb', line 29 def CollectionProxy.mutex @mutex end |
.pool ⇒ Object
Accessor for class-level instance variable that holds all the db-collection pairs that we know about, so that we can switch all of them globally.
22 23 24 |
# File 'lib/mongoid/collection_proxy.rb', line 22 def CollectionProxy.pool @pool end |
Instance Method Details
#synchronize(&block) ⇒ Object
Convenience method for synchronizing threads.
43 44 45 |
# File 'lib/mongoid/collection_proxy.rb', line 43 def synchronize(&block) CollectionProxy.mutex.synchronize(&block) end |