forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replset_database.rb
78 lines (72 loc) · 2.36 KB
/
replset_database.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
# encoding: utf-8
module Mongoid #:nodoc:
module Config #:nodoc:
class ReplsetDatabase < Hash
# Configure the database connections. This will return an array
# containing one Mongo::DB and nil (to keep compatibility with Mongoid::Config::Database)
# If you want the reads to go to a secondary node use the :read_secondary(true): option
#
# @example Configure the connection.
# db.configure
#
# @return [ Array<Mongo::DB, nil ] The Mongo databases.
#
# @since 2.0.0.rc.5
def configure
#yes, construction is weird but the driver wants "A list of host-port pairs ending with a hash containing any options"
#mongo likes symbols
options = self.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo}
connection = Mongo::ReplSetConnection.new(*(hosts << options))
if authenticating?
connection.add_auth(database, username, password)
connection.apply_saved_authentication
end
[ connection.db(database), nil ]
end
# Do we need to authenticate against the database?
#
# @example Are we authenticating?
# db.authenticating?
#
# @return [ true, false ] True if auth is needed, false if not.
#
# @since 2.0.2
def authenticating?
username || password
end
# Convenience for accessing the hash via dot notation.
#
# @example Access a value in alternate syntax.
# db.host
#
# @return [ Object ] The value in the hash.
#
# @since 2.0.2
def method_missing(name, *args, &block)
self[name.to_s]
end
# Create the new db configuration class.
#
# @example Initialize the class.
# Config::ReplsetDatabase.new(
# "hosts" => [[host1,port1],[host2,port2]]
# )
#
# replSet does not supports auth
#
# @param [ Hash ] options The configuration options.
#
# @option options [ Array ] :hosts The database host.
# @option options [ String ] :database The database name.
# @option options [ Boolean ] :read_secondary Tells the driver to read from secondaries.
# ...
#
# @see Mongo::ReplSetConnection for all options
#
# @since 2.0.0.rc.5
def initialize(options = {})
merge!(options)
end
end
end
end