module Mongoid::Sessions
Public Instance Methods
Clear all sessions from the current thread.
@example Clear all sessions.
Mongoid::Sessions.clear
@return [ Array ] The empty sessions.
@since 3.0.0
# File lib/mongoid/sessions.rb, line 95 def clear Threaded.sessions.clear end
Get the collection for this model from the session. Will check for an overridden collection name from the store_in macro or the collection with a pluralized model name.
@example Get the model's collection.
Model.collection
@return [ Moped::Collection ] The collection.
@since 3.0.0
# File lib/mongoid/sessions.rb, line 23 def collection self.class.collection end
Get the name of the collection this model persists to. This will be either the pluralized class name or the option defined in the store_in macro.
@example Get the collection name.
Model.collection_name
@return [ String ] The name of the collection.
@since 3.0.0
# File lib/mongoid/sessions.rb, line 37 def collection_name self.class.collection_name end
Get the default session.
@example Get the default session.
Mongoid::Sessions.default
@return [ Moped::Session ] The default session.
@since 3.0.0
# File lib/mongoid/sessions.rb, line 107 def default Threaded.sessions[:default] ||= Sessions::Factory.default end
Disconnect all active sessions.
@example Disconnect all active sessions.
Mongoid::Sessions.disconnect
@return [ true ] True.
@since 3.1.0
# File lib/mongoid/sessions.rb, line 119 def disconnect Threaded.sessions.values.each do |session| session.disconnect end end
Get the session for this model. This is determined in the following order:
1. Any custom configuration provided by the 'store_in' macro. 2. The 'default' session as provided in the mongoid.yml
@example Get the session.
model.mongo_session
@return [ Moped::Session ] The default moped session.
@since 3.0.0
# File lib/mongoid/sessions.rb, line 52 def mongo_session self.class.mongo_session end
Tell the next persistance operation to store in a specific collection, database or session.
@example Save the current document to a different collection.
model.with(collection: "secondary").save
@example Save the current document to a different database.
model.with(database: "secondary").save
@example Save the current document to a different session.
model.with(session: "replica_set").save
@example Save with a combination of options.
model.with(session: "sharded", database: "secondary").save
@param [ Hash ] options The storage options.
@option options [ String, Symbol ] :collection The collection name. @option options [ String, Symbol ] :database The database name. @option options [ String, Symbol ] :session The session name.
@return [ Document ] The current document.
@since 3.0.0
# File lib/mongoid/sessions.rb, line 80 def with(options) Threaded.set_persistence_options(self.class, options) self end
Get a session with the provided name.
@example Get a session with the name.
Mongoid::Sessions.with_name(:replica)
@param [ Symbol ] name The name of the session.
@return [ Moped::Session ] The named session.
@since 3.0.0
# File lib/mongoid/sessions.rb, line 135 def with_name(name) Threaded.sessions[name.to_sym] ||= Sessions::Factory.create(name) end