Module: Mongoid::Sessions

Extended by:
ActiveSupport::Concern
Includes:
Options
Included in:
Composable
Defined in:
lib/mongoid/sessions.rb,
lib/mongoid/sessions/factory.rb,
lib/mongoid/sessions/options.rb,
lib/mongoid/sessions/mongo_uri.rb,
lib/mongoid/sessions/validators/storage.rb

Defined Under Namespace

Modules: ClassMethods, Factory, Options, Validators Classes: MongoUri

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

#persistence_options, #with

Class Method Details

.clearArray

Clear all sessions from the current thread.

Examples:

Clear all sessions.

Mongoid::Sessions.clear

Returns:

  • (Array)

    The empty sessions.

Since:

  • 3.0.0



26
27
28
# File 'lib/mongoid/sessions.rb', line 26

def clear
  Threaded.sessions.clear
end

.defaultMoped::Session

Get the default session.

Examples:

Get the default session.

Mongoid::Sessions.default

Returns:

  • (Moped::Session)

    The default session.

Since:

  • 3.0.0



38
39
40
# File 'lib/mongoid/sessions.rb', line 38

def default
  Threaded.sessions[:default] ||= Sessions::Factory.default
end

.disconnecttrue

Disconnect all active sessions.

Examples:

Disconnect all active sessions.

Mongoid::Sessions.disconnect

Returns:

  • (true)

    True.

Since:

  • 3.1.0



50
51
52
53
54
# File 'lib/mongoid/sessions.rb', line 50

def disconnect
  Threaded.sessions.values.each do |session|
    session.disconnect
  end
end

.with_name(name) ⇒ Moped::Session

Get a session with the provided name.

Examples:

Get a session with the name.

Mongoid::Sessions.with_name(:replica)

Parameters:

  • name (Symbol)

    The name of the session.

Returns:

  • (Moped::Session)

    The named session.

Since:

  • 3.0.0



66
67
68
# File 'lib/mongoid/sessions.rb', line 66

def with_name(name)
  Threaded.sessions[name.to_sym] ||= Sessions::Factory.create(name)
end

Instance Method Details

#collectionMoped::Collection

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.

Examples:

Get the model’s collection.

Model.collection

Returns:

  • (Moped::Collection)

    The collection.

Since:

  • 3.0.0



81
82
83
# File 'lib/mongoid/sessions.rb', line 81

def collection
  mongo_session[collection_name]
end

#collection_nameObject



89
90
91
# File 'lib/mongoid/sessions.rb', line 89

def collection_name
  persistence_options.try { |opts| opts[:collection] } || self.class.collection_name
end

#mongo_sessionObject



85
86
87
# File 'lib/mongoid/sessions.rb', line 85

def mongo_session
  persistence_options ? self.class.mongo_session.with(persistence_options) : self.class.mongo_session
end