Module: Mongoid::Config

Extended by:
Config, Options
Includes:
ActiveModel::Observing
Included in:
Config
Defined in:
lib/mongoid/config.rb,
lib/mongoid/config/options.rb,
lib/mongoid/config/environment.rb,
lib/mongoid/config/validators/option.rb,
lib/mongoid/config/validators/session.rb

Overview

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

Defined Under Namespace

Modules: Environment, Options, Validators

Instance Method Summary collapse

Methods included from Options

defaults, option, reset, settings

Instance Method Details

#connect_to(name) ⇒ Object

Note:

Use only in development or test environments for convenience.

Connect to the provided database name on the default session.

Examples:

Set the database to connect to.

config.connect_to("mongoid_test")

Parameters:

  • name (String)

    The database name.

Since:

  • 3.0.0



38
39
40
41
42
43
44
45
# File 'lib/mongoid/config.rb', line 38

def connect_to(name)
  self.sessions = {
    default: {
      database: name,
      hosts: [ "localhost:27017" ]
    }
  }
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.



54
55
56
# File 'lib/mongoid/config.rb', line 54

def destructive_fields
  Components.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.

Since:

  • 2.0.1



68
69
70
71
72
# File 'lib/mongoid/config.rb', line 68

def load!(path, environment = nil)
  settings = Environment.load_yaml(path, environment)
  load_configuration(settings) if settings.present?
  settings
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.

Since:

  • 3.0.0



124
125
126
127
128
129
130
131
# File 'lib/mongoid/config.rb', line 124

def options=(options)
  if options
    options.each_pair do |option, value|
      Validators::Option.validate(option)
      send("#{option}=", value)
    end
  end
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.

Since:

  • 3.0.0



84
85
86
# File 'lib/mongoid/config.rb', line 84

def override_database(name)
  Threaded.database_override = name
end

#override_session(name) ⇒ String, Symbol

Override the session to use globally.

Examples:

Override the session globally.

config.override_session(:optional)

Parameters:

  • name (String, Symbol)

    The name of the session.

Returns:

  • (String, Symbol)

    The global override.

Since:

  • 3.0.0



98
99
100
# File 'lib/mongoid/config.rb', line 98

def override_session(name)
  Threaded.session_override = name ? name.to_s : nil
end

#purge!true

Purge all data in all collections, including indexes.

Examples:

Purge all data.

Mongoid::Config.purge!

Returns:

  • (true)

    true.

Since:

  • 2.0.2



110
111
112
113
114
# File 'lib/mongoid/config.rb', line 110

def purge!
  Sessions.default.collections.each do |collection|
    collection.drop
  end and true
end

#sessionsHash

Get the session configuration or an empty hash.

Examples:

Get the sessions configuration.

config.sessions

Returns:

  • (Hash)

    The sessions configuration.

Since:

  • 3.0.0



141
142
143
# File 'lib/mongoid/config.rb', line 141

def sessions
  @sessions ||= {}
end

#sessions=(sessions) ⇒ Object

Set the session configuration options.

Examples:

Set the session configuration options.

config.sessions = { default: { hosts: [ "localhost:27017" ] }}

Parameters:

  • sessions (Hash)

    The configuration options.

Raises:

Since:

  • 3.0.0



153
154
155
156
157
158
159
# File 'lib/mongoid/config.rb', line 153

def sessions=(sessions)
  raise Errors::NoSessionsConfig.new unless sessions
  sess = sessions.with_indifferent_access
  Validators::Session.validate(sess)
  @sessions = sess
  sess
end

#time_zoneString

Get the time zone to use.

Examples:

Get the time zone.

Config.time_zone

Returns:

  • (String)

    The time zone.

Since:

  • 3.0.0



169
170
171
# File 'lib/mongoid/config.rb', line 169

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