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/database.rb,
lib/mongoid/config/environment.rb,
lib/mongoid/config/replset_database.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Environment, Options Classes: Database, ReplsetDatabase

Constant Summary collapse

PRIVATE_OPTIONS =

keys to remove from self to not pass through to Mongo::Connection

%w(uri host hosts port database databases username
password logger use_activesupport_time_zone)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options

defaults, option, reset, settings

Instance Attribute Details

#masterMongo::DB Also known as: database

Returns the master database, or if none has been set it will raise an error.

Examples:

Get the master database.

config.master

Returns:

  • (Mongo::DB)

    The master database.

Raises:



20
21
22
# File 'lib/mongoid/config.rb', line 20

def master
  @master
end

#master The master database.(Themasterdatabase.) ⇒ Object



20
# File 'lib/mongoid/config.rb', line 20

attr_accessor :master

#use_activesupport_time_zoneObject Also known as: use_activesupport_time_zone?

Returns the value of attribute use_activesupport_time_zone.



192
193
194
# File 'lib/mongoid/config.rb', line 192

def use_activesupport_time_zone
  @use_activesupport_time_zone
end

Instance Method Details

#add_language(language_code = nil) ⇒ Object

Adds a new I18n locale file to the load path.

Examples:

Add a portuguese locale.

Mongoid::Config.add_language('pt')

Add all available languages.

Mongoid::Config.add_language('*')

Parameters:

  • language_code (String) (defaults to: nil)

    The language to add.



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

def add_language(language_code = nil)
  Dir[
    File.join(
      File.dirname(__FILE__), "..", "config", "locales", "#{language_code}.yml"
  )
  ].each do |file|
    I18n.load_path << File.expand_path(file)
  end
end

#blacklisted_optionsArray<String>

Get the blacklisted options from passing through to the driver.

Examples:

Get the blacklisted options.

Config.blacklisted_options

Returns:

Since:

  • 2.4.7



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

def blacklisted_options
  PRIVATE_OPTIONS + settings.keys.map(&:to_s)
end

#databasesHash

Get any extra databases that have been configured.

Examples:

Get the extras.

config.databases

Returns:

  • (Hash)

    A hash of secondary databases.



79
80
81
82
# File 'lib/mongoid/config.rb', line 79

def databases
  configure_extras(@settings["databases"]) unless @databases || !@settings
  @databases || {}
end

#databases=(databases) ⇒ Object

TODO:

Durran: There were no tests around the databases setter, not sure

what the exact expectation was. Set with a hash?



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

def databases=(databases)
end

#default_loggerLogger

Returns the default logger, which is either a Rails logger of stdout logger

Examples:

Get the default logger

config.default_logger

Returns:

  • (Logger)

    The default Logger instance.



136
137
138
# File 'lib/mongoid/config.rb', line 136

def default_logger
  defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new($stdout)
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:



96
97
98
# File 'lib/mongoid/config.rb', line 96

def destructive_fields
  Components.prohibited_methods
end

#from_hash(options = {}) ⇒ Object

Configure mongoid from a hash. This is usually called after parsing a yaml config file such as mongoid.yml.

Examples:

Configure Mongoid.

config.from_hash({})

Parameters:

  • options (Hash) (defaults to: {})

    The settings to use.



107
108
109
110
111
112
113
# File 'lib/mongoid/config.rb', line 107

def from_hash(options = {})
  options.except("database", "slaves", "databases").each_pair do |name, value|
    send("#{name}=", value) if respond_to?("#{name}=")
  end
  @master, @slaves = configure_databases(options)
  configure_extras(options["databases"])
end

#load!(path) ⇒ 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.

Since:

  • 2.0.1



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

def load!(path)
  Environment.load_yaml(path).tap do |settings|
    from_hash(settings) if settings.present?
  end
end

#loggerLogger

Returns the logger, or defaults to Rails logger or stdout logger.

Examples:

Get the logger.

config.logger

Returns:

  • (Logger)

    The configured logger or a default Logger instance.



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

def logger
  @logger = default_logger unless defined?(@logger)
  @logger
end

#logger=(logger) ⇒ Logger

Sets the logger for Mongoid to use.

Examples:

Set the logger.

config.logger = Logger.new($stdout, :warn)

Returns:

  • (Logger)

    The newly set logger.



157
158
159
160
161
162
163
164
# File 'lib/mongoid/config.rb', line 157

def logger=(logger)
  case logger
  when false, nil then @logger = nil
  when true then @logger = default_logger
  else
    @logger = logger if logger.respond_to?(:info)
  end
end

#purge!Object

Purge all data in all collections, including indexes.

Examples:

Purge all data.

Mongoid::Config.purge!

Since:

  • 2.0.2



172
173
174
175
176
# File 'lib/mongoid/config.rb', line 172

def purge!
  master.collections.map do |collection|
    collection.drop if collection.name !~ /system/
  end
end

#reconnect!(now = true) ⇒ Object

Convenience method for connecting to the master database after forking a new process.

Examples:

Reconnect to the master.

Mongoid.reconnect!

Parameters:

  • now (true, false) (defaults to: true)

    Perform the reconnection immediately?



241
242
243
244
245
246
247
248
249
# File 'lib/mongoid/config.rb', line 241

def reconnect!(now = true)
  if now
    master.connection.connect
  else
    # We set a @reconnect flag so that #master knows to reconnect the next
    # time the connection is accessed.
    @reconnect = true
  end
end