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

- (Object) connect_to(name)

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



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

def connect_to(name)
  self.sessions = {
    default: {
      database: name,
      hosts: [ "localhost:27017" ]
    }
  }
end

- (Array<String>) destructive_fields

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.



35
36
37
# File 'lib/mongoid/config.rb', line 35

def destructive_fields
  Components.prohibited_methods
end

- (Object) load!(path, environment = nil)

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



49
50
51
52
53
# File 'lib/mongoid/config.rb', line 49

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

- (Object) options=(options)

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



101
102
103
104
105
106
107
108
# File 'lib/mongoid/config.rb', line 101

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

- (true) purge!

TODO:

Durran: clean up.

Purge all data in all collections, including indexes.

Examples:

Purge all data.

Mongoid::Config.purge!

Returns:

  • (true)

    true.

Since:

  • 2.0.2



84
85
86
87
88
89
90
91
# File 'lib/mongoid/config.rb', line 84

def purge!
  session = Sessions.default
  collections = session["system.namespaces"].find(name: { "$not" => /system|\$/ }).to_a
  collections.each do |collection|
    _, name = collection["name"].split(".", 2)
    session[name].drop
  end and true
end

- (Hash) sessions

Get the session configuration or an empty hash.

Examples:

Get the sessions configuration.

config.sessions

Returns:

  • (Hash)

    The sessions configuration.

Since:

  • 3.0.0



118
119
120
# File 'lib/mongoid/config.rb', line 118

def sessions
  @sessions ||= {}
end

- (Object) sessions=(sessions)

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



130
131
132
133
134
135
136
# File 'lib/mongoid/config.rb', line 130

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

- (String) time_zone

Get the time zone to use.

Examples:

Get the time zone.

Config.time_zone

Returns:

  • (String)

    The time zone.

Since:

  • 3.0.0



146
147
148
# File 'lib/mongoid/config.rb', line 146

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