forked from mongodb/mongoid
/
config.rb
306 lines (279 loc) · 8.06 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
# encoding: utf-8
require "uri"
module Mongoid #:nodoc
class Config #:nodoc
include Singleton
attr_accessor \
:allow_dynamic_fields,
:reconnect_time,
:parameterize_keys,
:persist_in_safe_mode,
:raise_not_found_error,
:autocreate_indexes,
:use_object_ids,
:skip_version_check
# Initializes the configuration with default settings.
def initialize
reset
end
# Sets whether the times returned from the database are in UTC or local time.
# If you omit this setting, then times will be returned in
# the local time zone.
#
# Example:
#
# <tt>Config.use_utc = true</tt>
#
# Returns:
#
# A boolean
def use_utc=(value)
@use_utc = value || false
end
# Returns whether times are return from the database in UTC. If
# this setting is false, then times will be returned in the local time zone.
#
# Example:
#
# <tt>Config.use_utc</tt>
#
# Returns:
#
# A boolean
attr_reader :use_utc
alias_method :use_utc?, :use_utc
# 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:
#
# <tt>Config.master = Mongo::Connection.db("test")</tt>
#
# Returns:
#
# The master +Mongo::DB+ instance.
def master=(db)
check_database!(db)
@master = db
end
# Returns the master database, or if none has been set it will raise an
# error.
#
# Example:
#
# <tt>Config.master</tt>
#
# Returns:
#
# The master +Mongo::DB+
def master
@master || (raise Errors::InvalidDatabase.new(nil))
end
alias :database :master
alias :database= :master=
# Sets the Mongo::DB slave databases to be used. If the objects provided
# are not valid +Mongo::DBs+ an error will be raised.
#
# Example:
#
# <tt>Config.slaves = [ Mongo::Connection.db("test") ]</tt>
#
# Returns:
#
# The slave DB instances.
def slaves=(dbs)
return unless dbs
dbs.each do |db|
check_database!(db)
end
@slaves = dbs
end
# Returns the slave databases or nil if none have been set.
#
# Example:
#
# <tt>Config.slaves</tt>
#
# Returns:
#
# The slave +Mongo::DBs+
def slaves
@slaves
end
# Returns the logger, or defaults to Rails logger or stdout logger.
#
# Example:
#
# <tt>Config.logger</tt>
def logger
return @logger if defined?(@logger)
@logger = defined?(Rails) ? Rails.logger : ::Logger.new($stdout)
end
# Sets the logger for Mongoid to use.
#
# Example:
#
# <tt>Config.logger = Logger.new($stdout, :warn)</tt>
def logger=(logger)
@logger = logger
end
# Return field names that could cause destructive things to happen if
# defined in a Mongoid::Document
#
# Example:
#
# <tt>Config.destructive_fields</tt>
#
# Returns:
#
# An array of bad field names.
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
# Configure mongoid from a hash. This is usually called after parsing a
# yaml config file such as mongoid.yml.
#
# Example:
#
# <tt>Mongoid::Config.instance.from_hash({})</tt>
def from_hash(settings)
_master(settings)
_slaves(settings)
settings.except("database", "slaves").each_pair do |name, value|
send("#{name}=", value) if respond_to?(name)
end
end
# Convenience method for connecting to the master database after forking a
# new process.
#
# Example:
#
# <tt>Mongoid.reconnect!</tt>
def reconnect!
master.connection.connect_to_master
end
# Reset the configuration options to the defaults.
#
# Example:
#
# <tt>config.reset</tt>
def reset
@allow_dynamic_fields = true
@parameterize_keys = true
@persist_in_safe_mode = true
@raise_not_found_error = true
@reconnect_time = 3
@autocreate_indexes = false
@use_object_ids = false
@skip_version_check = false
@time_zone = nil
end
# Allows the override of autocreate_indexes via ENV variable
def autocreate_indexes
ENV["MONGOID_CREATE_INDEXES"] || @autocreate_indexes
end
##
# If Mongoid.use_object_ids = true
# Convert args to BSON::ObjectID
# If this args is an array, convert all args inside
# Else
# return args
#
# Options:
#
# args : A +String+ or an +Array+ convert to +BSON::ObjectID+
# cast : A +Boolean+ define if we can or not cast to BSON::ObjectID. If false, we use the default type of args
#
# Example:
#
# <tt>Mongoid.convert_to_object_id("4ab2bc4b8ad548971900005c", true)</tt>
# <tt>Mongoid.convert_to_object_id(["4ab2bc4b8ad548971900005c", "4ab2bc4b8ad548971900005d"])</tt>
#
# Returns:
#
# If Mongoid.use_object_ids = true
# An +Array+ of +BSON::ObjectID+ of each element if params is an +Array+
# A +BSON::ObjectID+ from params if params is +String+
# Else
# <tt>args</tt>
#
def convert_to_object_id(args, cast=true)
return args if !use_object_ids || args.is_a?(BSON::ObjectID) || !cast
if args.is_a?(String)
BSON::ObjectID(args)
else
args.map{ |a|
a.is_a?(BSON::ObjectID) ? a : BSON::ObjectID(a)
}
end
end
protected
# Check if the database is valid and the correct version.
#
# Example:
#
# <tt>config.check_database!</tt>
def check_database!(database)
raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
unless Mongoid.skip_version_check
version = database.connection.server_version
raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
end
end
# Get a master database from settings.
#
# TODO: Durran: This code's a bit hairy, refactor.
#
# Example:
#
# <tt>config._master({}, "test")</tt>
def _master(settings)
mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
host = settings["host"] || mongo_uri.host || "localhost"
port = settings["port"] || mongo_uri.port || 27017
pool_size = settings["pool_size"] || 1
username = settings["username"] || mongo_uri.user
password = settings["password"] || mongo_uri.password
connection = Mongo::Connection.new(host, port, :logger => Mongoid::Logger.new, :pool_size => pool_size)
if username || password
connection.add_auth(name, username, password)
connection.apply_saved_authentication
end
self.master = connection.db(name)
end
# Get a bunch-o-slaves from settings and names.
#
# TODO: Durran: This code's a bit hairy, refactor.
#
# Example:
#
# <tt>config._slaves({}, "test")</tt>
def _slaves(settings)
mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
self.slaves = []
slaves = settings["slaves"]
slaves.to_a.each do |slave|
slave_uri = slave["uri"].present? ? URI.parse(slave["uri"]) : OpenStruct.new
slave_username = slave["username"] || slave_uri.user
slave_password = slave["password"] || slave_uri.password
slave_connection = Mongo::Connection.new(
slave["host"] || slave_uri.host || "localhost",
slave["port"] || slave_uri.port,
:slave_ok => true
)
if slave_username || slave_password
slave_connection.add_auth(name, slave_username, slave_password)
slave_connection.apply_saved_authentication
end
self.slaves << slave_connection.db(name)
end
end
end
end