chhean-mongoid 2.0.1.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT_LICENSE +20 -0
- data/README.rdoc +49 -0
- data/lib/mongoid.rb +139 -0
- data/lib/mongoid/associations.rb +327 -0
- data/lib/mongoid/associations/embedded_in.rb +72 -0
- data/lib/mongoid/associations/embeds_many.rb +262 -0
- data/lib/mongoid/associations/embeds_one.rb +95 -0
- data/lib/mongoid/associations/foreign_key.rb +35 -0
- data/lib/mongoid/associations/meta_data.rb +29 -0
- data/lib/mongoid/associations/options.rb +73 -0
- data/lib/mongoid/associations/proxy.rb +33 -0
- data/lib/mongoid/associations/referenced_in.rb +71 -0
- data/lib/mongoid/associations/references_many.rb +243 -0
- data/lib/mongoid/associations/references_many_as_array.rb +78 -0
- data/lib/mongoid/associations/references_one.rb +116 -0
- data/lib/mongoid/attributes.rb +226 -0
- data/lib/mongoid/callbacks.rb +17 -0
- data/lib/mongoid/collection.rb +120 -0
- data/lib/mongoid/collections.rb +41 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +41 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/components.rb +32 -0
- data/lib/mongoid/config.rb +237 -0
- data/lib/mongoid/contexts.rb +24 -0
- data/lib/mongoid/contexts/enumerable.rb +151 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +285 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/criteria.rb +230 -0
- data/lib/mongoid/criterion/complex.rb +21 -0
- data/lib/mongoid/criterion/exclusion.rb +65 -0
- data/lib/mongoid/criterion/inclusion.rb +110 -0
- data/lib/mongoid/criterion/optional.rb +136 -0
- data/lib/mongoid/cursor.rb +82 -0
- data/lib/mongoid/deprecation.rb +22 -0
- data/lib/mongoid/dirty.rb +254 -0
- data/lib/mongoid/document.rb +264 -0
- data/lib/mongoid/errors.rb +124 -0
- data/lib/mongoid/extensions.rb +106 -0
- data/lib/mongoid/extensions/array/accessors.rb +17 -0
- data/lib/mongoid/extensions/array/aliasing.rb +4 -0
- data/lib/mongoid/extensions/array/assimilation.rb +26 -0
- data/lib/mongoid/extensions/array/conversions.rb +27 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +22 -0
- data/lib/mongoid/extensions/date/conversions.rb +24 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/accessors.rb +38 -0
- data/lib/mongoid/extensions/hash/assimilation.rb +39 -0
- data/lib/mongoid/extensions/hash/conversions.rb +45 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
- data/lib/mongoid/extensions/object/conversions.rb +27 -0
- data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +15 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +39 -0
- data/lib/mongoid/extensions/time_conversions.rb +35 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +80 -0
- data/lib/mongoid/fields.rb +61 -0
- data/lib/mongoid/finders.rb +144 -0
- data/lib/mongoid/identity.rb +39 -0
- data/lib/mongoid/indexes.rb +27 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/matchers.rb +35 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +26 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/memoization.rb +33 -0
- data/lib/mongoid/named_scope.rb +37 -0
- data/lib/mongoid/observable.rb +30 -0
- data/lib/mongoid/paths.rb +62 -0
- data/lib/mongoid/persistence.rb +218 -0
- data/lib/mongoid/persistence/command.rb +39 -0
- data/lib/mongoid/persistence/insert.rb +47 -0
- data/lib/mongoid/persistence/insert_embedded.rb +38 -0
- data/lib/mongoid/persistence/remove.rb +39 -0
- data/lib/mongoid/persistence/remove_all.rb +37 -0
- data/lib/mongoid/persistence/remove_embedded.rb +50 -0
- data/lib/mongoid/persistence/update.rb +63 -0
- data/lib/mongoid/railtie.rb +54 -0
- data/lib/mongoid/railties/database.rake +37 -0
- data/lib/mongoid/scope.rb +75 -0
- data/lib/mongoid/state.rb +32 -0
- data/lib/mongoid/timestamps.rb +27 -0
- data/lib/mongoid/validations.rb +51 -0
- data/lib/mongoid/validations/associated.rb +32 -0
- data/lib/mongoid/validations/locale/en.yml +4 -0
- data/lib/mongoid/validations/uniqueness.rb +50 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +27 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +41 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +24 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +15 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- metadata +284 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc
|
|
3
|
+
# The collections module is used for providing functionality around setting
|
|
4
|
+
# up and updating collections.
|
|
5
|
+
module Collections
|
|
6
|
+
extend ActiveSupport::Concern
|
|
7
|
+
included do
|
|
8
|
+
cattr_accessor :_collection, :collection_name
|
|
9
|
+
self.collection_name = self.name.collectionize
|
|
10
|
+
delegate :collection, :to => "self.class"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
module ClassMethods #:nodoc:
|
|
14
|
+
# Returns the collection associated with this +Document+. If the
|
|
15
|
+
# document is embedded, there will be no collection associated
|
|
16
|
+
# with it.
|
|
17
|
+
#
|
|
18
|
+
# Returns: <tt>Mongo::Collection</tt>
|
|
19
|
+
def collection
|
|
20
|
+
raise Errors::InvalidCollection.new(self) if embedded?
|
|
21
|
+
self._collection || set_collection
|
|
22
|
+
add_indexes; self._collection
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Macro for setting the collection name to store in.
|
|
26
|
+
#
|
|
27
|
+
# Example:
|
|
28
|
+
#
|
|
29
|
+
# <tt>Person.store_in :populdation</tt>
|
|
30
|
+
def store_in(name)
|
|
31
|
+
self.collection_name = name.to_s
|
|
32
|
+
set_collection
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
protected
|
|
36
|
+
def set_collection
|
|
37
|
+
self._collection = Mongoid::Collection.new(self, self.collection_name)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Collections #:nodoc:
|
|
4
|
+
class CyclicIterator
|
|
5
|
+
|
|
6
|
+
attr_reader :counter
|
|
7
|
+
|
|
8
|
+
# Performs iteration over an array, if the array gets to the end then loop
|
|
9
|
+
# back to the first.
|
|
10
|
+
#
|
|
11
|
+
# Example:
|
|
12
|
+
#
|
|
13
|
+
# <tt>CyclicIterator.new([ first, second ])</tt>
|
|
14
|
+
def initialize(array)
|
|
15
|
+
@array, @counter = array, -1
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Get the next element in the array. If the element is the last in the
|
|
19
|
+
# array then return the first.
|
|
20
|
+
#
|
|
21
|
+
# Example:
|
|
22
|
+
#
|
|
23
|
+
# <tt>iterator.next</tt>
|
|
24
|
+
#
|
|
25
|
+
# Returns:
|
|
26
|
+
#
|
|
27
|
+
# The next element in the array.
|
|
28
|
+
def next
|
|
29
|
+
(@counter == @array.size - 1) ? @counter = 0 : @counter = @counter + 1
|
|
30
|
+
@array[@counter]
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Collections #:nodoc:
|
|
4
|
+
class Master
|
|
5
|
+
|
|
6
|
+
attr_reader :collection
|
|
7
|
+
|
|
8
|
+
# All read and write operations should delegate to the master connection.
|
|
9
|
+
# These operations mimic the methods on a Mongo:Collection.
|
|
10
|
+
#
|
|
11
|
+
# Example:
|
|
12
|
+
#
|
|
13
|
+
# <tt>collection.save({ :name => "Al" })</tt>
|
|
14
|
+
Operations::ALL.each do |name|
|
|
15
|
+
define_method(name) { |*args| collection.send(name, *args) }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Create the new database writer. Will create a collection from the
|
|
19
|
+
# master database.
|
|
20
|
+
#
|
|
21
|
+
# Example:
|
|
22
|
+
#
|
|
23
|
+
# <tt>Master.new(master, "mongoid_people")</tt>
|
|
24
|
+
def initialize(master, name)
|
|
25
|
+
@collection = master.collection(name)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Collections #:nodoc:
|
|
4
|
+
module Operations #:nodoc:
|
|
5
|
+
# Constant definining all the read operations available for a
|
|
6
|
+
# Mongo:Collection. This is used in delegation.
|
|
7
|
+
READ = [
|
|
8
|
+
:[],
|
|
9
|
+
:db,
|
|
10
|
+
:count,
|
|
11
|
+
:distinct,
|
|
12
|
+
:find,
|
|
13
|
+
:find_one,
|
|
14
|
+
:group,
|
|
15
|
+
:index_information,
|
|
16
|
+
:map_reduce,
|
|
17
|
+
:mapreduce,
|
|
18
|
+
:options
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
# Constant definining all the write operations available for a
|
|
22
|
+
# Mongo:Collection. This is used in delegation.
|
|
23
|
+
WRITE = [
|
|
24
|
+
:<<,
|
|
25
|
+
:create_index,
|
|
26
|
+
:drop,
|
|
27
|
+
:drop_index,
|
|
28
|
+
:drop_indexes,
|
|
29
|
+
:insert,
|
|
30
|
+
:remove,
|
|
31
|
+
:rename,
|
|
32
|
+
:save,
|
|
33
|
+
:update
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
# Convenience constant for getting back all collection operations.
|
|
37
|
+
ALL = (READ + WRITE)
|
|
38
|
+
PROXIED = ALL - [ :find, :find_one, :map_reduce, :mapreduce ]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Collections #:nodoc:
|
|
4
|
+
class Slaves
|
|
5
|
+
|
|
6
|
+
attr_reader :iterator
|
|
7
|
+
|
|
8
|
+
# All read operations should delegate to the slave connections.
|
|
9
|
+
# These operations mimic the methods on a Mongo:Collection.
|
|
10
|
+
#
|
|
11
|
+
# Example:
|
|
12
|
+
#
|
|
13
|
+
# <tt>collection.save({ :name => "Al" })</tt>
|
|
14
|
+
Operations::READ.each do |name|
|
|
15
|
+
define_method(name) { |*args| collection.send(name, *args) }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Is the collection of slaves empty or not?
|
|
19
|
+
#
|
|
20
|
+
# Return:
|
|
21
|
+
#
|
|
22
|
+
# True is the iterator is not set, false if not.
|
|
23
|
+
def empty?
|
|
24
|
+
@iterator.nil?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Create the new database reader. Will create a collection from the
|
|
28
|
+
# slave databases and cycle through them on each read.
|
|
29
|
+
#
|
|
30
|
+
# Example:
|
|
31
|
+
#
|
|
32
|
+
# <tt>Reader.new(slaves, "mongoid_people")</tt>
|
|
33
|
+
def initialize(slaves, name)
|
|
34
|
+
unless slaves.blank?
|
|
35
|
+
@iterator = CyclicIterator.new(slaves.collect { |db| db.collection(name) })
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
protected
|
|
40
|
+
def collection
|
|
41
|
+
@iterator.next
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc
|
|
3
|
+
module Components #:nodoc
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
included do
|
|
6
|
+
# All modules that a +Document+ is composed of are defined in this
|
|
7
|
+
# module, to keep the document class from getting too cluttered.
|
|
8
|
+
include ActiveModel::Conversion
|
|
9
|
+
include ActiveModel::Naming
|
|
10
|
+
include ActiveModel::Serialization
|
|
11
|
+
include ActiveModel::Serializers::JSON
|
|
12
|
+
include Mongoid::Associations
|
|
13
|
+
include Mongoid::Attributes
|
|
14
|
+
include Mongoid::Callbacks
|
|
15
|
+
include Mongoid::Collections
|
|
16
|
+
include Mongoid::Dirty
|
|
17
|
+
include Mongoid::Extras
|
|
18
|
+
include Mongoid::Fields
|
|
19
|
+
include Mongoid::Indexes
|
|
20
|
+
include Mongoid::Matchers
|
|
21
|
+
include Mongoid::Memoization
|
|
22
|
+
include Mongoid::Observable
|
|
23
|
+
include Mongoid::Paths
|
|
24
|
+
include Mongoid::Persistence
|
|
25
|
+
include Mongoid::State
|
|
26
|
+
include Mongoid::Validations
|
|
27
|
+
extend ActiveModel::Translation
|
|
28
|
+
extend Mongoid::Finders
|
|
29
|
+
extend Mongoid::NamedScope
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "uri"
|
|
3
|
+
|
|
4
|
+
module Mongoid #:nodoc
|
|
5
|
+
class Config #:nodoc
|
|
6
|
+
include Singleton
|
|
7
|
+
|
|
8
|
+
attr_accessor \
|
|
9
|
+
:allow_dynamic_fields,
|
|
10
|
+
:reconnect_time,
|
|
11
|
+
:parameterize_keys,
|
|
12
|
+
:persist_in_safe_mode,
|
|
13
|
+
:raise_not_found_error,
|
|
14
|
+
:use_object_ids,
|
|
15
|
+
:skip_version_check
|
|
16
|
+
|
|
17
|
+
# Defaults the configuration options to true.
|
|
18
|
+
def initialize
|
|
19
|
+
reset
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Sets whether the times returned from the database are in UTC or local time.
|
|
23
|
+
# If you omit this setting, then times will be returned in
|
|
24
|
+
# the local time zone.
|
|
25
|
+
#
|
|
26
|
+
# Example:
|
|
27
|
+
#
|
|
28
|
+
# <tt>Config.use_utc = true</tt>
|
|
29
|
+
#
|
|
30
|
+
# Returns:
|
|
31
|
+
#
|
|
32
|
+
# A boolean
|
|
33
|
+
def use_utc=(value)
|
|
34
|
+
@use_utc = value || false
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Returns whether times are return from the database in UTC. If
|
|
38
|
+
# this setting is false, then times will be returned in the local time zone.
|
|
39
|
+
#
|
|
40
|
+
# Example:
|
|
41
|
+
#
|
|
42
|
+
# <tt>Config.use_utc</tt>
|
|
43
|
+
#
|
|
44
|
+
# Returns:
|
|
45
|
+
#
|
|
46
|
+
# A boolean
|
|
47
|
+
attr_reader :use_utc
|
|
48
|
+
alias_method :use_utc?, :use_utc
|
|
49
|
+
|
|
50
|
+
# Sets the Mongo::DB master database to be used. If the object trying to be
|
|
51
|
+
# set is not a valid +Mongo::DB+, then an error will be raised.
|
|
52
|
+
#
|
|
53
|
+
# Example:
|
|
54
|
+
#
|
|
55
|
+
# <tt>Config.master = Mongo::Connection.db("test")</tt>
|
|
56
|
+
#
|
|
57
|
+
# Returns:
|
|
58
|
+
#
|
|
59
|
+
# The Master DB instance.
|
|
60
|
+
def master=(db)
|
|
61
|
+
check_database!(db)
|
|
62
|
+
@master = db
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Returns the master database, or if none has been set it will raise an
|
|
66
|
+
# error.
|
|
67
|
+
#
|
|
68
|
+
# Example:
|
|
69
|
+
#
|
|
70
|
+
# <tt>Config.master</tt>
|
|
71
|
+
#
|
|
72
|
+
# Returns:
|
|
73
|
+
#
|
|
74
|
+
# The master +Mongo::DB+
|
|
75
|
+
def master
|
|
76
|
+
@master || (raise Errors::InvalidDatabase.new(nil))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
alias :database :master
|
|
80
|
+
alias :database= :master=
|
|
81
|
+
|
|
82
|
+
# Sets the Mongo::DB slave databases to be used. If the objects trying to me
|
|
83
|
+
# set are not valid +Mongo::DBs+, then an error will be raise.
|
|
84
|
+
#
|
|
85
|
+
# Example:
|
|
86
|
+
#
|
|
87
|
+
# <tt>Config.slaves = [ Mongo::Connection.db("test") ]</tt>
|
|
88
|
+
#
|
|
89
|
+
# Returns:
|
|
90
|
+
#
|
|
91
|
+
# The slaves DB instances.
|
|
92
|
+
def slaves=(dbs)
|
|
93
|
+
return unless dbs
|
|
94
|
+
dbs.each do |db|
|
|
95
|
+
check_database!(db)
|
|
96
|
+
end
|
|
97
|
+
@slaves = dbs
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Returns the slave databases, or if none has been set nil
|
|
101
|
+
#
|
|
102
|
+
# Example:
|
|
103
|
+
#
|
|
104
|
+
# <tt>Config.slaves</tt>
|
|
105
|
+
#
|
|
106
|
+
# Returns:
|
|
107
|
+
#
|
|
108
|
+
# The slave +Mongo::DBs+
|
|
109
|
+
def slaves
|
|
110
|
+
@slaves
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Return field names that could cause destructive things to happen if
|
|
114
|
+
# defined in a Mongoid::Document
|
|
115
|
+
#
|
|
116
|
+
# Example:
|
|
117
|
+
#
|
|
118
|
+
# <tt>Config.destructive_fields</tt>
|
|
119
|
+
#
|
|
120
|
+
# Returns:
|
|
121
|
+
#
|
|
122
|
+
# An array of bad field names.
|
|
123
|
+
def destructive_fields
|
|
124
|
+
@destructive_fields ||= lambda {
|
|
125
|
+
klass = Class.new do
|
|
126
|
+
include Mongoid::Document
|
|
127
|
+
end
|
|
128
|
+
klass.instance_methods(true).collect { |method| method.to_s }
|
|
129
|
+
}.call
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Configure mongoid from a hash that was usually parsed out of yml.
|
|
133
|
+
#
|
|
134
|
+
# Example:
|
|
135
|
+
#
|
|
136
|
+
# <tt>Mongoid::Config.instance.from_hash({})</tt>
|
|
137
|
+
def from_hash(settings)
|
|
138
|
+
_master(settings)
|
|
139
|
+
_slaves(settings)
|
|
140
|
+
settings.except("database").each_pair do |name, value|
|
|
141
|
+
send("#{name}=", value) if respond_to?(name)
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Reset the configuration options to the defaults.
|
|
146
|
+
#
|
|
147
|
+
# Example:
|
|
148
|
+
#
|
|
149
|
+
# <tt>config.reset</tt>
|
|
150
|
+
def reset
|
|
151
|
+
@allow_dynamic_fields = true
|
|
152
|
+
@parameterize_keys = true
|
|
153
|
+
@persist_in_safe_mode = true
|
|
154
|
+
@raise_not_found_error = true
|
|
155
|
+
@reconnect_time = 3
|
|
156
|
+
@use_object_ids = false
|
|
157
|
+
@skip_version_check = false
|
|
158
|
+
@time_zone = nil
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
protected
|
|
162
|
+
|
|
163
|
+
# Check if the database is valid and the correct version.
|
|
164
|
+
#
|
|
165
|
+
# Example:
|
|
166
|
+
#
|
|
167
|
+
# <tt>config.check_database!</tt>
|
|
168
|
+
def check_database!(database)
|
|
169
|
+
raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
|
|
170
|
+
unless Mongoid.skip_version_check
|
|
171
|
+
version = database.connection.server_version
|
|
172
|
+
raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Get a Rails logger or stdout logger.
|
|
177
|
+
#
|
|
178
|
+
# Example:
|
|
179
|
+
#
|
|
180
|
+
# <tt>config.logger</tt>
|
|
181
|
+
def logger
|
|
182
|
+
defined?(Rails) ? Rails.logger : Logger.new($stdout)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Get a master database from settings.
|
|
186
|
+
#
|
|
187
|
+
# Example:
|
|
188
|
+
#
|
|
189
|
+
# <tt>config._master({}, "test")</tt>
|
|
190
|
+
def _master(settings)
|
|
191
|
+
mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
|
|
192
|
+
|
|
193
|
+
name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
|
|
194
|
+
host = settings["host"] || mongo_uri.host || "localhost"
|
|
195
|
+
port = settings["port"] || mongo_uri.port || 27017
|
|
196
|
+
pool_size = settings["pool_size"] || 1
|
|
197
|
+
username = settings["username"] || mongo_uri.user
|
|
198
|
+
password = settings["password"] || mongo_uri.password
|
|
199
|
+
|
|
200
|
+
connection = Mongo::Connection.new(host, port, :logger => logger, :pool_size => pool_size)
|
|
201
|
+
if username || password
|
|
202
|
+
connection.add_auth(name, username, password)
|
|
203
|
+
connection.apply_saved_authentication
|
|
204
|
+
end
|
|
205
|
+
self.master = connection.db(name)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Get a bunch-o-slaves from settings and names.
|
|
209
|
+
#
|
|
210
|
+
# Example:
|
|
211
|
+
#
|
|
212
|
+
# <tt>config._slaves({}, "test")</tt>
|
|
213
|
+
def _slaves(settings)
|
|
214
|
+
mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
|
|
215
|
+
name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
|
|
216
|
+
self.slaves = []
|
|
217
|
+
slaves = settings["slaves"]
|
|
218
|
+
slaves.to_a.each do |slave|
|
|
219
|
+
slave_uri = slave["uri"].present? ? URI.parse(slave["uri"]) : OpenStruct.new
|
|
220
|
+
slave_username = slave["username"] || slave_uri.user
|
|
221
|
+
slave_password = slave["password"] || slave_uri.password
|
|
222
|
+
|
|
223
|
+
slave_connection = Mongo::Connection.new(
|
|
224
|
+
slave["host"] || slave_uri.host || "localhost",
|
|
225
|
+
slave["port"] || slave_uri.port,
|
|
226
|
+
:slave_ok => true
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
if slave_username || slave_password
|
|
230
|
+
slave_connection.add_auth(name, slave_username, slave_password)
|
|
231
|
+
slave_connection.apply_saved_authentication
|
|
232
|
+
end
|
|
233
|
+
self.slaves << slave_connection.db(name)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
end
|