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
-
#master ⇒ Mongo::DB
(also: #database)
Returns the master database, or if none has been set it will raise an error.
-
#settings ⇒ Object
Returns the value of attribute settings.
-
#use_activesupport_time_zone ⇒ Object
(also: #use_activesupport_time_zone?)
Sets whether the times returned from the database use the ruby or the ActiveSupport time zone.
Instance Method Summary collapse
-
#add_language(language_code = nil) ⇒ Object
Adds a new I18n locale file to the load path.
-
#databases ⇒ Hash
Get any extra databases that have been configured.
-
#databases=(databases) ⇒ Object
what the exact expectation was.
-
#default_logger ⇒ Logger
Returns the default logger, which is either a Rails logger of stdout logger.
-
#destructive_fields ⇒ Array<String>
Return field names that could cause destructive things to happen if defined in a Mongoid::Document.
-
#from_hash(options = {}) ⇒ Object
Configure mongoid from a hash.
-
#load!(path) ⇒ Object
Load the settings from a compliant mongoid.yml file.
-
#logger ⇒ Logger
Returns the logger, or defaults to Rails logger or stdout logger.
-
#logger=(logger) ⇒ Logger
Sets the logger for Mongoid to use.
-
#option(name, options = {}) ⇒ Object
Define a configuration option with a default.
-
#purge! ⇒ Object
Purge all data in all collections, including indexes.
-
#reconnect!(now = true) ⇒ Object
Convenience method for connecting to the master database after forking a new process.
-
#reset ⇒ Object
Reset the configuration options to the defaults.
-
#slaves ⇒ Object
deprecated
Deprecated.
User replica sets instead.
-
#slaves=(dbs) ⇒ Object
deprecated
Deprecated.
User replica sets instead.
Instance Attribute Details
#master ⇒ Mongo::DB Also known as: database
Returns the master database, or if none has been set it will raise an error.
233 234 235 |
# File 'lib/mongoid/config.rb', line 233 def master @master end |
#settings ⇒ Object
Returns the value of attribute settings.
16 17 18 |
# File 'lib/mongoid/config.rb', line 16 def settings @settings end |
#use_activesupport_time_zone ⇒ Object Also known as: use_activesupport_time_zone?
Sets whether the times returned from the database use the ruby or the ActiveSupport time zone. If the setting is false, then times will use the ruby time zone.
Example:
Config.use_activesupport_time_zone
Returns:
A boolean
204 205 206 |
# File 'lib/mongoid/config.rb', line 204 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.
61 62 63 64 65 66 67 68 69 |
# File 'lib/mongoid/config.rb', line 61 def add_language(language_code = nil) Dir[ File.join( File.dirname(__FILE__), "..", "config", "locales", "#{language_code}.yml" ) ].each do |file| I18n.load_path << File.(file) end end |
#databases ⇒ Hash
Get any extra databases that have been configured.
77 78 79 80 |
# File 'lib/mongoid/config.rb', line 77 def databases configure_extras(@settings["databases"]) unless @databases || !@settings @databases || {} end |
#databases=(databases) ⇒ Object
Durran: There were no tests around the databases setter, not sure
what the exact expectation was. Set with a hash?
84 85 |
# File 'lib/mongoid/config.rb', line 84 def databases=(databases) end |
#default_logger ⇒ Logger
Returns the default logger, which is either a Rails logger of stdout logger
141 142 143 |
# File 'lib/mongoid/config.rb', line 141 def default_logger defined?(Rails) ? Rails.logger : ::Logger.new($stdout) end |
#destructive_fields ⇒ Array<String>
Return field names that could cause destructive things to happen if defined in a Mongoid::Document.
94 95 96 97 98 99 100 101 |
# File 'lib/mongoid/config.rb', line 94 def destructive_fields @destructive_fields ||= lambda { klass = Class.new do include Mongoid::Document end klass.instance_methods(true).collect { |method| method.to_s } }.call end |
#from_hash(options = {}) ⇒ Object
Configure mongoid from a hash. This is usually called after parsing a yaml config file such as mongoid.yml.
110 111 112 113 114 115 116 |
# File 'lib/mongoid/config.rb', line 110 def from_hash( = {}) .except("database", "slaves", "databases").each_pair do |name, value| send("#{name}=", value) if respond_to?("#{name}=") end @master, @slaves = configure_databases() configure_extras(["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.
127 128 129 130 131 132 133 |
# File 'lib/mongoid/config.rb', line 127 def load!(path) environment = defined?(Rails) ? Rails.env : ENV["RACK_ENV"] settings = YAML.load(ERB.new(File.new(path).read).result)[environment] if settings.present? from_hash(settings) end end |
#logger ⇒ Logger
Returns the logger, or defaults to Rails logger or stdout logger.
151 152 153 154 |
# File 'lib/mongoid/config.rb', line 151 def logger @logger = default_logger unless defined?(@logger) @logger end |
#logger=(logger) ⇒ Logger
Sets the logger for Mongoid to use.
162 163 164 |
# File 'lib/mongoid/config.rb', line 162 def logger=(logger) @logger = logger end |
#option(name, options = {}) ⇒ Object
Define a configuration option with a default.
30 31 32 33 34 35 36 |
# File 'lib/mongoid/config.rb', line 30 def option(name, = {}) define_method(name) do settings.has_key?(name) ? settings[name] : [:default] end define_method("#{name}=") { |value| settings[name] = value } define_method("#{name}?") { send(name) } end |
#purge! ⇒ Object
Purge all data in all collections, including indexes.
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.
253 254 255 256 257 258 259 260 261 |
# File 'lib/mongoid/config.rb', line 253 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 |
#reset ⇒ Object
Reset the configuration options to the defaults.
267 268 269 |
# File 'lib/mongoid/config.rb', line 267 def reset settings.clear end |
#slaves ⇒ Object
User replica sets instead.
272 273 274 |
# File 'lib/mongoid/config.rb', line 272 def slaves slave_warning! end |
#slaves=(dbs) ⇒ Object
User replica sets instead.
277 278 279 |
# File 'lib/mongoid/config.rb', line 277 def slaves=(dbs) slave_warning! end |