Module: Mongoid::Sessions

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/mongoid/sessions.rb,
lib/mongoid/sessions/factory.rb,
lib/mongoid/sessions/validators/storage.rb

Defined Under Namespace

Modules: ClassMethods, Factory, Validators

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Array) clear

Clear all sessions from the current thread.

Examples:

Clear all sessions.

Mongoid::Sessions.clear

Returns:

  • (Array)

    The empty sessions.

Since:

  • 3.0.0



96
97
98
# File 'lib/mongoid/sessions.rb', line 96

def clear
  Threaded.sessions.clear
end

+ (Moped::Session) default

Get the default session.

Examples:

Get the default session.

Mongoid::Sessions.default

Returns:

  • (Moped::Session)

    The default session.

Since:

  • 3.0.0



108
109
110
# File 'lib/mongoid/sessions.rb', line 108

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

+ (Moped::Session) with_name(name)

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



122
123
124
# File 'lib/mongoid/sessions.rb', line 122

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

Instance Method Details

- (Moped::Collection) 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



24
25
26
# File 'lib/mongoid/sessions.rb', line 24

def collection
  self.class.collection
end

- (String) collection_name

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.

Examples:

Get the collection name.

Model.collection_name

Returns:

  • (String)

    The name of the collection.

Since:

  • 3.0.0



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

def collection_name
  self.class.collection_name
end

- (Moped::Session) mongo_session

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

Examples:

Get the session.

model.mongo_session

Returns:

  • (Moped::Session)

    The default moped session.

Since:

  • 3.0.0



53
54
55
# File 'lib/mongoid/sessions.rb', line 53

def mongo_session
  self.class.mongo_session
end

- (Document) with(options)

Tell the next persistance operation to store in a specific collection, database or session.

Examples:

Save the current document to a different collection.

model.with(collection: "secondary").save

Save the current document to a different database.

model.with(database: "secondary").save

Save the current document to a different session.

model.with(session: "replica_set").save

Save with a combination of options.

model.with(session: "sharded", database: "secondary").save

Parameters:

  • options (Hash)

    The storage options.

Options Hash (options):

  • :collection (String, Symbol)

    The collection name.

  • :database (String, Symbol)

    The database name.

  • :session (String, Symbol)

    The session name.

Returns:

Since:

  • 3.0.0



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

def with(options)
  Threaded.set_persistence_options(self.class, options)
  self
end