Module: Mongoid::Config

Extended by:
Config
Includes:
ActiveModel::Observing
Included in:
Config
Defined in:
lib/mongoid/config.rb,
lib/mongoid/config/database.rb,
lib/mongoid/config/replset_database.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Database, ReplsetDatabase

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



16
17
18
# File 'lib/mongoid/config.rb', line 16

def defaults
  @defaults
end

#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:



231
232
233
# File 'lib/mongoid/config.rb', line 231

def master
  @master
end

#settingsObject

Returns the value of attribute settings.



16
17
18
# File 'lib/mongoid/config.rb', line 16

def settings
  @settings
end

#use_activesupport_time_zoneObject Also known as: use_activesupport_time_zone?

Returns the value of attribute use_activesupport_time_zone.



202
203
204
# File 'lib/mongoid/config.rb', line 202

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.



71
72
73
74
75
76
77
78
79
# File 'lib/mongoid/config.rb', line 71

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

#databasesHash

Get any extra databases that have been configured.

Examples:

Get the extras.

config.databases

Returns:

  • (Hash)

    A hash of secondary databases.



87
88
89
90
# File 'lib/mongoid/config.rb', line 87

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?



94
95
# File 'lib/mongoid/config.rb', line 94

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.



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

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:



104
105
106
# File 'lib/mongoid/config.rb', line 104

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.



115
116
117
118
119
120
121
# File 'lib/mongoid/config.rb', line 115

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



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

def load!(path)
  environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"]
  settings = YAML.load(ERB.new(File.new(path).read).result)[environment]
  if settings.present?
    from_hash(settings)
  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.



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

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.



167
168
169
170
171
172
173
174
# File 'lib/mongoid/config.rb', line 167

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

#option(name, options = {}) ⇒ Object

Define a configuration option with a default.

Examples:

Define the option.

Config.option(:persist_in_safe_mode, :default => false)

Parameters:

  • name (Symbol)

    The name of the configuration option.

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

    Extras for the option.

Options Hash (options):

  • :default (Object)

    The default value.

Since:

  • 2.0.0.rc.1



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongoid/config.rb', line 31

def option(name, options = {})
  defaults[name] = settings[name] = options[:default]

  class_eval <<-RUBY
    def #{name}
      settings[#{name.inspect}]
    end

    def #{name}=(value)
      settings[#{name.inspect}] = value
    end

    def #{name}?
      #{name}
    end
  RUBY
end

#purge!Object

Purge all data in all collections, including indexes.

Examples:

Purge all data.

Mongoid::Config.purge!

Since:

  • 2.0.2



182
183
184
185
186
# File 'lib/mongoid/config.rb', line 182

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?



251
252
253
254
255
256
257
258
259
# File 'lib/mongoid/config.rb', line 251

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

#resetObject

Reset the configuration options to the defaults.

Examples:

Reset the configuration options.

config.reset


265
266
267
# File 'lib/mongoid/config.rb', line 265

def reset
  settings.replace(defaults)
end

#slavesObject

Deprecated.

User replica sets instead.



270
271
272
# File 'lib/mongoid/config.rb', line 270

def slaves
  slave_warning!
end

#slaves=(dbs) ⇒ Object

Deprecated.

User replica sets instead.



275
276
277
# File 'lib/mongoid/config.rb', line 275

def slaves=(dbs)
  slave_warning!
end