Module: Mongoid::Config

Extended by:
Forwardable, Config, Defaults, Options
Included in:
Config
Defined in:
build/mongoid-master/lib/mongoid/config.rb,
build/mongoid-master/lib/mongoid/config/options.rb,
build/mongoid-master/lib/mongoid/config/defaults.rb,
build/mongoid-master/lib/mongoid/config/environment.rb,
build/mongoid-master/lib/mongoid/config/validators/client.rb,
build/mongoid-master/lib/mongoid/config/validators/option.rb,
build/mongoid-master/lib/mongoid/config/validators/async_query_executor.rb

Overview

This module defines all the configuration options for Mongoid, including the database connections.

Defined Under Namespace

Modules: Defaults, Environment, Options, Validators

Constant Summary collapse

LOCK =
Mutex.new

Instance Method Summary collapse

Methods included from Options

defaults, log_level, option, reset, settings

Methods included from Defaults

load_defaults

Instance Method Details

#clientsHash

Get the client configuration or an empty hash.

Examples:

Get the clients configuration.

config.clients

Returns:

  • (Hash)

    The clients configuration.



338
339
340
# File 'build/mongoid-master/lib/mongoid/config.rb', line 338

def clients
  @clients ||= {}
end

#configself

Returns the Config singleton, for use in the configure DSL.

Returns:

  • (self)

    The Config singleton.



156
157
158
# File 'build/mongoid-master/lib/mongoid/config.rb', line 156

def config
  self
end

#configured?true | false

Has Mongoid been configured? This is checking that at least a valid client config exists.

Examples:

Is Mongoid configured?

config.configured?

Returns:

  • (true | false)

    If Mongoid is configured.



167
168
169
# File 'build/mongoid-master/lib/mongoid/config.rb', line 167

def configured?
  clients.key?(:default)
end

#connect_to(name, options = { read: { mode: :primary }}) ⇒ Object

Note:

Use only in development or test environments for convenience.

Connect to the provided database name on the default client.

Examples:

Set the database to connect to.

config.connect_to("mongoid_test")

Parameters:

  • name (String)

    The database name.



179
180
181
182
183
184
185
186
187
# File 'build/mongoid-master/lib/mongoid/config.rb', line 179

def connect_to(name, options = { read: { mode: :primary }})
  self.clients = {
    default: {
      database: name,
      hosts: [ "localhost:27017" ],
      options: options
    }
  }
end

#deregister_model(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deregister a model in the application with Mongoid.

Parameters:

  • klass (Class)

    The model to deregister.



246
247
248
249
250
# File 'build/mongoid-master/lib/mongoid/config.rb', line 246

def deregister_model(klass)
  LOCK.synchronize do
    models.delete(klass)
  end
end

#destructive_fieldsArray<String>

Return field names that could cause destructive things to happen if defined in a Mongoid::Document.

Examples:

Get the destructive fields.

config.destructive_fields

Returns:

  • (Array<String>)

    An array of bad field names.



196
197
198
# File 'build/mongoid-master/lib/mongoid/config.rb', line 196

def destructive_fields
  Composable.prohibited_methods
end

#load!(path, environment = nil) ⇒ Object

Load the settings from a compliant mongoid.yml file. This can be used for easy setup with frameworks other than Rails.

Examples:

Configure Mongoid.

Mongoid.load!("/path/to/mongoid.yml")

Parameters:

  • path (String)

    The path to the file.

  • environment (String | Symbol) (defaults to: nil)

    The environment to load.



208
209
210
211
212
213
214
215
216
# File 'build/mongoid-master/lib/mongoid/config.rb', line 208

def load!(path, environment = nil)
  settings = Environment.load_yaml(path, environment)
  if settings.present?
    Clients.disconnect
    Clients.clear
    load_configuration(settings)
  end
  settings
end

#load_configuration(settings) ⇒ Object

From a hash of settings, load all the configuration.

Examples:

Load the configuration.

config.load_configuration(settings)

Parameters:

  • settings (Hash)

    The configuration settings.



258
259
260
261
262
263
264
# File 'build/mongoid-master/lib/mongoid/config.rb', line 258

def load_configuration(settings)
  configuration = settings.with_indifferent_access
  self.options = configuration[:options]
  self.clients = configuration[:clients]
  Mongo.options = configuration[:driver_options] || {}
  set_log_levels
end

#modelsArray<Class>

Get all the models in the application - this is everything that includes Mongoid::Document.

Examples:

Get all the models.

config.models

Returns:

  • (Array<Class>)

    All the models in the application.



225
226
227
# File 'build/mongoid-master/lib/mongoid/config.rb', line 225

def models
  @models ||= []
end

#options=(options) ⇒ Object

Set the configuration options. Will validate each one individually.

Examples:

Set the options.

config.options = { raise_not_found_error: true }

Parameters:

  • options (Hash)

    The configuration options.



322
323
324
325
326
327
328
329
330
# File 'build/mongoid-master/lib/mongoid/config.rb', line 322

def options=(options)
  if options
    Validators::AsyncQueryExecutor.validate(options)
    options.each_pair do |option, value|
      Validators::Option.validate(option)
      send("#{option}=", value)
    end
  end
end

#override_client(name) ⇒ String | Symbol

Override the client to use globally.

Examples:

Override the client globally.

config.override_client(:optional)

Parameters:

  • name (String | Symbol)

    The name of the client.

Returns:

  • (String | Symbol)

    The global override.



286
287
288
# File 'build/mongoid-master/lib/mongoid/config.rb', line 286

def override_client(name)
  Threaded.client_override = name ? name.to_s : nil
end

#override_database(name) ⇒ String | Symbol

Override the database to use globally.

Examples:

Override the database globally.

config.override_database(:optional)

Parameters:

  • name (String | Symbol)

    The name of the database.

Returns:

  • (String | Symbol)

    The global override.



274
275
276
# File 'build/mongoid-master/lib/mongoid/config.rb', line 274

def override_database(name)
  Threaded.database_override = name
end

#purge!true

Note:

This is the fastest way to drop all data.

Purge all data in all collections, including indexes.

Examples:

Purge all data.

Mongoid::Config.purge!

Returns:

  • (true)

    true.



298
299
300
# File 'build/mongoid-master/lib/mongoid/config.rb', line 298

def purge!
  global_client.database.collections.each(&:drop) and true
end

#register_model(klass) ⇒ Object

Register a model in the application with Mongoid.

Examples:

Register a model.

config.register_model(Band)

Parameters:

  • klass (Class)

    The model to register.



235
236
237
238
239
# File 'build/mongoid-master/lib/mongoid/config.rb', line 235

def register_model(klass)
  LOCK.synchronize do
    models.push(klass) unless models.include?(klass)
  end
end

#running_with_passenger?true | false

Is the application running under passenger?

Examples:

Is the application using passenger?

config.running_with_passenger?

Returns:

  • (true | false)

    If the app is deployed on Passenger.



358
359
360
# File 'build/mongoid-master/lib/mongoid/config.rb', line 358

def running_with_passenger?
  @running_with_passenger ||= defined?(PhusionPassenger)
end

#time_zoneString

Get the time zone to use.

Examples:

Get the time zone.

Config.time_zone

Returns:

  • (String)

    The time zone.



348
349
350
# File 'build/mongoid-master/lib/mongoid/config.rb', line 348

def time_zone
  use_utc? ? "UTC" : ::Time.zone
end

#truncate!true

Note:

This will be slower than purge!

Truncate all data in all collections, but not the indexes.

Examples:

Truncate all collection data.

Mongoid::Config.truncate!

Returns:

  • (true)

    true.



310
311
312
313
314
# File 'build/mongoid-master/lib/mongoid/config.rb', line 310

def truncate!
  global_client.database.collections.each do |collection|
    collection.find.delete_many
  end and true
end