forked from mongodb/mongoid
/
config.rb
345 lines (315 loc) · 10.3 KB
/
config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# encoding: utf-8
require "uri"
require "mongoid/config/database"
require "mongoid/config/replset_database"
module Mongoid #:nodoc
# This module defines all the configuration options for Mongoid, including the
# database connections.
#
# @todo Durran: This module needs an overhaul, remove singleton, etc.
module Config
extend self
include ActiveModel::Observing
attr_accessor :master, :settings, :defaults
@settings = {}
@defaults = {}
# Define a configuration option with a default.
#
# @example Define the option.
# Config.option(:persist_in_safe_mode, :default => false)
#
# @param [ Symbol ] name The name of the configuration option.
# @param [ Hash ] options Extras for the option.
#
# @option options [ Object ] :default The default value.
#
# @since 2.0.0.rc.1
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
option :allow_dynamic_fields, :default => true
option :autocreate_indexes, :default => false
option :identity_map_enabled, :default => false
option :include_root_in_json, :default => false
option :max_retries_on_connection_failure, :default => 0
option :parameterize_keys, :default => true
option :persist_in_safe_mode, :default => false
option :preload_models, :default => false
option :raise_not_found_error, :default => true
option :skip_version_check, :default => false
option :time_zone, :default => nil
option :use_utc, :default => false
# Adds a new I18n locale file to the load path.
#
# @example Add a portuguese locale.
# Mongoid::Config.add_language('pt')
#
# @example Add all available languages.
# Mongoid::Config.add_language('*')
#
# @param [ String ] language_code The language to add.
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
# Get any extra databases that have been configured.
#
# @example Get the extras.
# config.databases
#
# @return [ Hash ] A hash of secondary databases.
def databases
configure_extras(@settings["databases"]) unless @databases || !@settings
@databases || {}
end
# @todo Durran: There were no tests around the databases setter, not sure
# what the exact expectation was. Set with a hash?
def databases=(databases)
end
# Return field names that could cause destructive things to happen if
# defined in a Mongoid::Document.
#
# @example Get the destructive fields.
# config.destructive_fields
#
# @return [ Array<String> ] An array of bad field names.
def destructive_fields
Components.prohibited_methods
end
# Configure mongoid from a hash. This is usually called after parsing a
# yaml config file such as mongoid.yml.
#
# @example Configure Mongoid.
# config.from_hash({})
#
# @param [ Hash ] options The settings to use.
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 the settings from a compliant mongoid.yml file. This can be used for
# easy setup with frameworks other than Rails.
#
# @example Configure Mongoid.
# Mongoid.load!("/path/to/mongoid.yml")
#
# @param [ String ] path The path to the file.
#
# @since 2.0.1
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
# Returns the default logger, which is either a Rails logger of stdout logger
#
# @example Get the default logger
# config.default_logger
#
# @return [ Logger ] The default Logger instance.
def default_logger
defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new($stdout)
end
# Returns the logger, or defaults to Rails logger or stdout logger.
#
# @example Get the logger.
# config.logger
#
# @return [ Logger ] The configured logger or a default Logger instance.
def logger
@logger = default_logger unless defined?(@logger)
@logger
end
# Sets the logger for Mongoid to use.
#
# @example Set the logger.
# config.logger = Logger.new($stdout, :warn)
#
# @return [ Logger ] The newly set logger.
def logger=(logger)
case logger
when Logger then @logger = logger
when false, nil then @logger = nil
end
end
# Purge all data in all collections, including indexes.
#
# @example Purge all data.
# Mongoid::Config.purge!
#
# @since 2.0.2
def purge!
master.collections.map do |collection|
collection.drop if collection.name !~ /system/
end
end
# Sets whether the times returned from the database use the ruby or
# the ActiveSupport time zone.
#
# @note If you omit this setting, then times will use the ruby time zone.
#
# @example Set the time zone config.
# Config.use_activesupport_time_zone = true
#
# @param [ true, false ] value Whether to use Active Support time zones.
#
# @return [ true, false ] The supplied value or false if nil.
def use_activesupport_time_zone=(value)
@use_activesupport_time_zone = value || false
end
attr_reader :use_activesupport_time_zone
alias_method :use_activesupport_time_zone?, :use_activesupport_time_zone
# Sets the Mongo::DB master database to be used. If the object trying to be
# set is not a valid +Mongo::DB+, then an error will be raised.
#
# @example Set the master database.
# config.master = Mongo::Connection.new.db("test")
#
# @param [ Mongo::DB ] db The master database.
#
# @raise [ Errors::InvalidDatabase ] If the master isnt a valid object.
#
# @return [ Mongo::DB ] The master instance.
def master=(db)
check_database!(db)
@master = db
end
alias :database= :master=
# Returns the master database, or if none has been set it will raise an
# error.
#
# @example Get the master database.
# config.master
#
# @raise [ Errors::InvalidDatabase ] If the database was not set.
#
# @return [ Mongo::DB ] The master database.
def master
unless @master
@master, @slaves = configure_databases(@settings) unless @settings.blank?
raise Errors::InvalidDatabase.new(nil) unless @master
end
if @reconnect
@reconnect = false
reconnect!
end
@master
end
alias :database :master
# Convenience method for connecting to the master database after forking a
# new process.
#
# @example Reconnect to the master.
# Mongoid.reconnect!
#
# @param [ true, false ] now Perform the reconnection immediately?
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 the configuration options to the defaults.
#
# @example Reset the configuration options.
# config.reset
def reset
settings.replace(defaults)
end
# @deprecated User replica sets instead.
def slaves
slave_warning!
end
# @deprecated User replica sets instead.
def slaves=(dbs)
slave_warning!
end
protected
# Check if the database is valid and the correct version.
#
# @example Check if the database is valid.
# config.check_database!
#
# @param [ Mongo::DB ] database The db to check.
#
# @raise [ Errors::InvalidDatabase ] If the object is not valid.
# @raise [ Errors::UnsupportedVersion ] If the db version is too old.
def check_database!(database)
raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
unless skip_version_check
version = database.connection.server_version
raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
end
end
# Get a database from settings.
#
# @example Configure the master and slave dbs.
# config.configure_databases("database" => "mongoid")
#
# @param [ Hash ] options The options to use.
#
# @option options [ String ] :database The database name.
# @option options [ String ] :host The database host.
# @option options [ String ] :password The password for authentication.
# @option options [ Integer ] :port The port for the database.
# @option options [ Array<Hash> ] :slaves The slave db options.
# @option options [ String ] :uri The uri for the database.
# @option options [ String ] :username The user for authentication.
#
# @since 2.0.0.rc.1
def configure_databases(options)
if options.has_key?('hosts')
ReplsetDatabase.new(options).configure
else
Database.new(options).configure
end
end
# Get the secondary databases from settings.
#
# @example Configure the master and slave dbs.
# config.configure_extras("databases" => settings)
#
# @param [ Hash ] options The options to use.
#
# @since 2.0.0.rc.1
def configure_extras(extras)
@databases = (extras || []).inject({}) do |dbs, (name, options)|
dbs.tap do |extra|
dbs[name], dbs["#{name}_slaves"] = configure_databases(options)
end
end
end
# Temporarily here so people can move to replica sets.
def slave_warning!
warn(
"Using Mongoid for traditional slave databases will be removed in the " +
"next release in preference of replica sets. Please change your setup " +
"accordingly."
)
end
end
end