Module: Mongoid::Clients::Options

Extended by:
ActiveSupport::Concern
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



58
59
60
61
62
# File 'lib/mongoid/clients/options.rb', line 58

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

#mongo_clientObject



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

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

#persistence_optionsObject



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

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 persistence 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



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

def with(options)
  @persistence_options = options
  self
end