Module: Mongoid::Clients::Options::ClassMethods

Includes:
Threaded
Defined in:
lib/mongoid/clients/options.rb

Instance Method Summary collapse

Methods included from Threaded

#client_with_options, #persistence_options

Instance Method Details

#client_nameObject



207
208
209
210
211
212
# File 'lib/mongoid/clients/options.rb', line 207

def client_name
  if persistence_options && v = persistence_options[:client]
    return v.to_sym
  end
  super
end

#collection_nameObject



214
215
216
217
218
219
# File 'lib/mongoid/clients/options.rb', line 214

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

#database_nameObject



221
222
223
224
225
226
# File 'lib/mongoid/clients/options.rb', line 221

def database_name
  if persistence_options && v = persistence_options[:database]
    return v.to_sym
  end
  super
end

#with(options) ⇒ Class

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

Examples:

Create a document in a different collection.

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

Create a document in a different database.

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

Create a document in a different client.

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

Create with a combination of options.

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

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:

  • (Class)

    The model class.

Since:

  • 3.0.0



252
253
254
255
256
257
258
259
260
261
# File 'lib/mongoid/clients/options.rb', line 252

def with(options)
  if block_given?
    set_options(self, options)
    result = yield self
    unset_options(self)
    result
  else
    Proxy.new(self, (persistence_options || {}).merge(options))
  end
end