Module: Mongoid::Clients::Options

Extended by:
ActiveSupport::Concern, Gem::Deprecate
Included in:
Mongoid::Clients, Mongoid::Criteria
Defined in:
lib/mongoid/clients/options.rb

Defined Under Namespace

Modules: ClassMethods, Threaded Classes: Proxy

Instance Method Summary collapse

Instance Method Details

#collection_nameObject


60
61
62
63
64
# File 'lib/mongoid/clients/options.rb', line 60

def collection_name
  if persistence_options && v = persistence_options[:collection]
    return v.to_sym
  end
end

#mongo_clientObject Also known as: mongo_session


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mongoid/clients/options.rb', line 46

def mongo_client
  if persistence_options
    if persistence_options[:client]
      client = Clients.with_name(persistence_options[:client])
    else
      client = Clients.with_name(self.class.client_name)
      client.use(self.class.database_name)
    end
    client.with(persistence_options.reject{ |k, v| k == :collection || k == :client })
  end
end

#persistence_optionsObject


42
43
44
# File 'lib/mongoid/clients/options.rb', line 42

def persistence_options
  @persistence_options
end

#with(options) ⇒ Document

Note:

This method will instantiate a new client under the covers and can be expensive. It is also recommended that the user manually closes the extra client after using it, otherwise an excessive amount of connections to the server will be eventually opened.

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

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 client.

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

Save with a combination of options.

model.with(client: "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.

  • :client (String, Symbol)

    The client name.

Returns:

Since:

  • 3.0.0


37
38
39
40
# File 'lib/mongoid/clients/options.rb', line 37

def with(options)
  @persistence_options = options
  self
end