module Mongoid::Sessions::ClassMethods

Public Instance Methods

clear_persistence_options() click to toggle source

Clear all persistence options from the current thread.

@example Clear the persistence options.

Mongoid::Sessions.clear_persistence_options

@return [ true ] True.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 150
def clear_persistence_options
  Threaded.clear_persistence_options(self)
end
collection() click to toggle source

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 164
def collection
  if opts = persistence_options
    coll = mongo_session.with(opts)[opts[:collection] || collection_name]
    clear_persistence_options unless validating_with_query? || _loading_revision?
    coll
  else
    mongo_session[collection_name]
  end
end
collection_name() click to toggle source

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 [ Symbol ] The name of the collection.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 184
def collection_name
  __collection_name__
end
database_name() click to toggle source

Get the default database name for this model.

@example Get the default database name.

Model.database_name

@return [ Symbol ] The name of the database.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 196
def database_name
  __database_name__
end
database_override() click to toggle source

Get the overridden database name. This either can be overridden by using Model.with or by overriding at the global level via +Mongoid.override_database(:name)+.

@example Get the overridden database name.

Model.database_override

@return [ String, Symbol ] The overridden database name.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 210
def database_override
  persistence_options.try { |opts| opts[:database] } || Threaded.database_override
end
mongo_session() click to toggle source

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 225
def mongo_session
  session = __session__
  session.use(database_override || current_database_name(session))
  session
end
persistence_options() click to toggle source

Get the persistence options from the current thread.

@example Get the persistence options.

Model.persistence_options

@return [ Hash ] The persistence options.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 239
def persistence_options
  Threaded.persistence_options(self)
end
session_override() click to toggle source

Get the overridden session name. This either can be overridden by using Model.with or by overriding at the global level via +Mongoid.override_session(:name)+.

@example Get the overridden session name.

Model.session_override

@return [ String, Symbol ] The overridden session name.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 253
def session_override
  persistence_options.try { |opts| opts[:session] } || Threaded.session_override
end
store_in(options) click to toggle source

Give this model specific custom default storage options.

@example Store this model by default in “artists”

class Band
  include Mongoid::Document
  store_in collection: "artists"
end

@example Store this model by default in the sharded db.

class Band
  include Mongoid::Document
  store_in database: "echo_shard"
end

@example Store this model by default in a different session.

class Band
  include Mongoid::Document
  store_in session: "secondary"
end

@example Store this model with a combination of options.

class Band
  include Mongoid::Document
  store_in collection: "artists", database: "secondary"
end

@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 [ Class ] The model class.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 292
def store_in(options)
  Validators::Storage.validate(self, options)
  self.storage_options ||= {}
  self.storage_options.merge!(options)
end
with(options) click to toggle source

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

@example Create a document in a different collection.

Model.with(collection: "secondary").create(name: "test")

@example Create a document in a different database.

Model.with(database: "secondary").create(name: "test")

@example Create a document in a different session.

Model.with(session: "secondary").create(name: "test")

@example Create with a combination of options.

Model.with(session: "sharded", database: "secondary").create

@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 [ Class ] The model class.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 322
def with(options)
  Threaded.set_persistence_options(self, options)
  self
end

Private Instance Methods

__collection_name__() click to toggle source

Get the name of the collection this model persists to.

@example Get the collection name.

Model.__collection_name__

@return [ Symbol ] The name of the collection.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 337
def __collection_name__
  if storage_options && name = storage_options[:collection]
    __evaluate__(name)
  else
    default_collection_name
  end
end
__database_name__() click to toggle source

Get the database name for the model.

@example Get the database name.

Model.__database_name__

@return [ Symbol ] The name of the database.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 353
def __database_name__
  if storage_options && name = storage_options[:database]
    __evaluate__(name)
  else
    Mongoid.sessions[__session_name__][:database]
  end
end
__evaluate__(name) click to toggle source

Eval the provided value, either byt calling it if it responds to call or returning the value itself.

@api private

@example Evaluate the name.

Model.__evaluate__(:name)

@param [ String, Symbol, Proc ] name The name.

@return [ Symbol ] The value as a symbol.

@since 3.1.0

# File lib/mongoid/sessions.rb, line 406
def __evaluate__(name)
  name.respond_to?(:call) ? name.call.to_sym : name.to_sym
end
__session__() click to toggle source

Get the session for this class.

@example Get the session.

Model.__session__

@return [ Moped::Session ] The moped session.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 385
def __session__
  if !(name = session_override).nil?
    Sessions.with_name(name)
  else
    Sessions.with_name(__session_name__)
  end
end
__session_name__() click to toggle source

Get the session name for the model.

@example Get the session name.

Model.__session_name__

@return [ Symbol ] The name of the session.

@since 3.0.0

# File lib/mongoid/sessions.rb, line 369
def __session_name__
  if storage_options && name = storage_options[:session]
    __evaluate__(name)
  else
    :default
  end
end
current_database_name(session) click to toggle source

Get the name of the current database to use. Will check for a session override with a database first, then database name.

@api private

@example Get the current database name.

Model.current_database_name

@param [ Moped::Session ] session The current session.

@return [ Symbol ] The current database name.

@since 3.1.0

# File lib/mongoid/sessions.rb, line 423
def current_database_name(session)
  if session_override && name = session.options[:database]
    name
  else
    database_name
  end
end