stonegao-mongoid 2.0.0.rc.6
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/LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +51 -0
- data/lib/config/locales/bg.yml +44 -0
- data/lib/config/locales/de.yml +44 -0
- data/lib/config/locales/en.yml +45 -0
- data/lib/config/locales/es.yml +44 -0
- data/lib/config/locales/fr.yml +45 -0
- data/lib/config/locales/hu.yml +47 -0
- data/lib/config/locales/it.yml +42 -0
- data/lib/config/locales/kr.yml +68 -0
- data/lib/config/locales/nl.yml +42 -0
- data/lib/config/locales/pl.yml +42 -0
- data/lib/config/locales/pt-br.yml +43 -0
- data/lib/config/locales/pt.yml +43 -0
- data/lib/config/locales/ro.yml +49 -0
- data/lib/config/locales/sv.yml +43 -0
- data/lib/config/locales/zh-CN.yml +34 -0
- data/lib/mongoid/atomicity.rb +111 -0
- data/lib/mongoid/attributes.rb +251 -0
- data/lib/mongoid/callbacks.rb +13 -0
- data/lib/mongoid/collection.rb +137 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +42 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/collections.rb +70 -0
- data/lib/mongoid/components.rb +45 -0
- data/lib/mongoid/config/database.rb +167 -0
- data/lib/mongoid/config/replset_database.rb +48 -0
- data/lib/mongoid/config.rb +343 -0
- data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
- data/lib/mongoid/contexts/enumerable.rb +226 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +345 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/contexts.rb +21 -0
- data/lib/mongoid/copyable.rb +44 -0
- data/lib/mongoid/criteria.rb +325 -0
- data/lib/mongoid/criterion/complex.rb +34 -0
- data/lib/mongoid/criterion/creational.rb +34 -0
- data/lib/mongoid/criterion/exclusion.rb +67 -0
- data/lib/mongoid/criterion/inclusion.rb +134 -0
- data/lib/mongoid/criterion/inspection.rb +20 -0
- data/lib/mongoid/criterion/optional.rb +213 -0
- data/lib/mongoid/criterion/selector.rb +74 -0
- data/lib/mongoid/cursor.rb +81 -0
- data/lib/mongoid/default_scope.rb +28 -0
- data/lib/mongoid/dirty.rb +251 -0
- data/lib/mongoid/document.rb +256 -0
- data/lib/mongoid/errors/document_not_found.rb +29 -0
- data/lib/mongoid/errors/invalid_collection.rb +19 -0
- data/lib/mongoid/errors/invalid_database.rb +20 -0
- data/lib/mongoid/errors/invalid_field.rb +19 -0
- data/lib/mongoid/errors/invalid_options.rb +16 -0
- data/lib/mongoid/errors/invalid_type.rb +26 -0
- data/lib/mongoid/errors/mongoid_error.rb +27 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
- data/lib/mongoid/errors/unsaved_document.rb +23 -0
- data/lib/mongoid/errors/unsupported_version.rb +21 -0
- data/lib/mongoid/errors/validations.rb +24 -0
- data/lib/mongoid/errors.rb +12 -0
- data/lib/mongoid/extensions/array/conversions.rb +23 -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 +27 -0
- data/lib/mongoid/extensions/date/conversions.rb +25 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/false_class/equality.rb +13 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/conversions.rb +19 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
- data/lib/mongoid/extensions/object/conversions.rb +25 -0
- data/lib/mongoid/extensions/object/reflections.rb +17 -0
- data/lib/mongoid/extensions/object/yoda.rb +27 -0
- data/lib/mongoid/extensions/object_id/conversions.rb +57 -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 +34 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +38 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extensions.rb +116 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +95 -0
- data/lib/mongoid/fields.rb +138 -0
- data/lib/mongoid/finders.rb +173 -0
- data/lib/mongoid/hierarchy.rb +85 -0
- data/lib/mongoid/identity.rb +89 -0
- data/lib/mongoid/indexes.rb +38 -0
- data/lib/mongoid/inspection.rb +58 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/json.rb +16 -0
- data/lib/mongoid/keys.rb +77 -0
- data/lib/mongoid/logger.rb +18 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +27 -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/matchers.rb +55 -0
- data/lib/mongoid/modifiers/command.rb +18 -0
- data/lib/mongoid/modifiers/inc.rb +24 -0
- data/lib/mongoid/modifiers.rb +24 -0
- data/lib/mongoid/multi_database.rb +11 -0
- data/lib/mongoid/multi_parameter_attributes.rb +80 -0
- data/lib/mongoid/named_scope.rb +36 -0
- data/lib/mongoid/nested_attributes.rb +43 -0
- data/lib/mongoid/paranoia.rb +103 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence/command.rb +59 -0
- data/lib/mongoid/persistence/insert.rb +53 -0
- data/lib/mongoid/persistence/insert_embedded.rb +42 -0
- data/lib/mongoid/persistence/remove.rb +44 -0
- data/lib/mongoid/persistence/remove_all.rb +40 -0
- data/lib/mongoid/persistence/remove_embedded.rb +48 -0
- data/lib/mongoid/persistence/update.rb +76 -0
- data/lib/mongoid/persistence.rb +237 -0
- data/lib/mongoid/railtie.rb +129 -0
- data/lib/mongoid/railties/database.rake +171 -0
- data/lib/mongoid/railties/document.rb +12 -0
- data/lib/mongoid/relations/accessors.rb +157 -0
- data/lib/mongoid/relations/auto_save.rb +34 -0
- data/lib/mongoid/relations/binding.rb +26 -0
- data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
- data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
- data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
- data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
- data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +99 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
- data/lib/mongoid/relations/bindings.rb +9 -0
- data/lib/mongoid/relations/builder.rb +42 -0
- data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
- data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
- data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
- data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
- data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
- data/lib/mongoid/relations/builders.rb +79 -0
- data/lib/mongoid/relations/cascading/delete.rb +19 -0
- data/lib/mongoid/relations/cascading/destroy.rb +19 -0
- data/lib/mongoid/relations/cascading/nullify.rb +18 -0
- data/lib/mongoid/relations/cascading/strategy.rb +26 -0
- data/lib/mongoid/relations/cascading.rb +55 -0
- data/lib/mongoid/relations/constraint.rb +45 -0
- data/lib/mongoid/relations/cyclic.rb +97 -0
- data/lib/mongoid/relations/embedded/in.rb +173 -0
- data/lib/mongoid/relations/embedded/many.rb +483 -0
- data/lib/mongoid/relations/embedded/one.rb +170 -0
- data/lib/mongoid/relations/macros.rb +306 -0
- data/lib/mongoid/relations/many.rb +171 -0
- data/lib/mongoid/relations/metadata.rb +533 -0
- data/lib/mongoid/relations/nested_builder.rb +68 -0
- data/lib/mongoid/relations/one.rb +47 -0
- data/lib/mongoid/relations/polymorphic.rb +54 -0
- data/lib/mongoid/relations/proxy.rb +128 -0
- data/lib/mongoid/relations/referenced/in.rb +216 -0
- data/lib/mongoid/relations/referenced/many.rb +443 -0
- data/lib/mongoid/relations/referenced/many_to_many.rb +344 -0
- data/lib/mongoid/relations/referenced/one.rb +206 -0
- data/lib/mongoid/relations/reflections.rb +45 -0
- data/lib/mongoid/relations.rb +105 -0
- data/lib/mongoid/safe.rb +23 -0
- data/lib/mongoid/safety.rb +207 -0
- data/lib/mongoid/scope.rb +31 -0
- data/lib/mongoid/serialization.rb +99 -0
- data/lib/mongoid/state.rb +66 -0
- data/lib/mongoid/timestamps.rb +38 -0
- data/lib/mongoid/validations/associated.rb +42 -0
- data/lib/mongoid/validations/uniqueness.rb +85 -0
- data/lib/mongoid/validations.rb +117 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +51 -0
- data/lib/mongoid.rb +139 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +23 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +17 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- data/lib/rails/mongoid.rb +57 -0
- metadata +380 -0
|
@@ -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,42 @@
|
|
|
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
|
+
:stats,
|
|
19
|
+
:options
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
# Constant definining all the write operations available for a
|
|
23
|
+
# Mongo:Collection. This is used in delegation.
|
|
24
|
+
WRITE = [
|
|
25
|
+
:<<,
|
|
26
|
+
:create_index,
|
|
27
|
+
:drop,
|
|
28
|
+
:drop_index,
|
|
29
|
+
:drop_indexes,
|
|
30
|
+
:insert,
|
|
31
|
+
:remove,
|
|
32
|
+
:rename,
|
|
33
|
+
:save,
|
|
34
|
+
:update
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
# Convenience constant for getting back all collection operations.
|
|
38
|
+
ALL = (READ + WRITE)
|
|
39
|
+
PROXIED = ALL - [ :find, :find_one, :map_reduce, :mapreduce ]
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
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,70 @@
|
|
|
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
|
+
|
|
11
|
+
delegate :collection, :db, :to => "self.class"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module ClassMethods #:nodoc:
|
|
15
|
+
# Returns the collection associated with this +Document+. If the
|
|
16
|
+
# document is embedded, there will be no collection associated
|
|
17
|
+
# with it.
|
|
18
|
+
#
|
|
19
|
+
# Returns: <tt>Mongo::Collection</tt>
|
|
20
|
+
def collection
|
|
21
|
+
self._collection || set_collection
|
|
22
|
+
add_indexes; self._collection
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Return the database associated with this collection.
|
|
26
|
+
#
|
|
27
|
+
# Example:
|
|
28
|
+
#
|
|
29
|
+
# <tt>Person.db</tt>
|
|
30
|
+
def db
|
|
31
|
+
collection.db
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Convenience method for getting index information from the collection.
|
|
35
|
+
#
|
|
36
|
+
# Example:
|
|
37
|
+
#
|
|
38
|
+
# <tt>Person.index_information</tt>
|
|
39
|
+
def index_information
|
|
40
|
+
collection.index_information
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The MongoDB logger is not exposed through the driver to be changed
|
|
44
|
+
# after initialization of the connection, this is a hacky way around that
|
|
45
|
+
# if logging needs to be changed at runtime.
|
|
46
|
+
#
|
|
47
|
+
# Example:
|
|
48
|
+
#
|
|
49
|
+
# <tt>Person.logger = Logger.new($stdout)</tt>
|
|
50
|
+
def logger=(logger)
|
|
51
|
+
db.connection.instance_variable_set(:@logger, logger)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Macro for setting the collection name to store in.
|
|
55
|
+
#
|
|
56
|
+
# Example:
|
|
57
|
+
#
|
|
58
|
+
# <tt>Person.store_in :population</tt>
|
|
59
|
+
def store_in(name)
|
|
60
|
+
self.collection_name = name.to_s
|
|
61
|
+
set_collection
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
protected
|
|
65
|
+
def set_collection
|
|
66
|
+
self._collection = Mongoid::Collection.new(self, self.collection_name)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc
|
|
3
|
+
module Components #:nodoc
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
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
|
+
included do
|
|
9
|
+
extend ActiveModel::Translation
|
|
10
|
+
extend Mongoid::DefaultScope
|
|
11
|
+
extend Mongoid::Finders
|
|
12
|
+
extend Mongoid::NamedScope
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
include ActiveModel::Conversion
|
|
16
|
+
include ActiveModel::Naming
|
|
17
|
+
include ActiveModel::MassAssignmentSecurity
|
|
18
|
+
include ActiveModel::Serializers::JSON
|
|
19
|
+
include ActiveModel::Serializers::Xml
|
|
20
|
+
include Mongoid::Atomicity
|
|
21
|
+
include Mongoid::Attributes
|
|
22
|
+
include Mongoid::Collections
|
|
23
|
+
include Mongoid::Copyable
|
|
24
|
+
include Mongoid::Dirty
|
|
25
|
+
include Mongoid::Extras
|
|
26
|
+
include Mongoid::Fields
|
|
27
|
+
include Mongoid::Hierarchy
|
|
28
|
+
include Mongoid::Indexes
|
|
29
|
+
include Mongoid::Inspection
|
|
30
|
+
include Mongoid::JSON
|
|
31
|
+
include Mongoid::Keys
|
|
32
|
+
include Mongoid::Matchers
|
|
33
|
+
include Mongoid::MultiParameterAttributes
|
|
34
|
+
include Mongoid::Modifiers
|
|
35
|
+
include Mongoid::NestedAttributes
|
|
36
|
+
include Mongoid::Paths
|
|
37
|
+
include Mongoid::Persistence
|
|
38
|
+
include Mongoid::Relations
|
|
39
|
+
include Mongoid::Safety
|
|
40
|
+
include Mongoid::Serialization
|
|
41
|
+
include Mongoid::State
|
|
42
|
+
include Mongoid::Validations
|
|
43
|
+
include Mongoid::Callbacks
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Config #:nodoc:
|
|
4
|
+
|
|
5
|
+
# This class handles the configuration and initialization of a mongodb
|
|
6
|
+
# database from options.
|
|
7
|
+
class Database < Hash
|
|
8
|
+
|
|
9
|
+
# Configure the database connections. This will return an array
|
|
10
|
+
# containing the master and an array of slaves.
|
|
11
|
+
#
|
|
12
|
+
# @example Configure the connection.
|
|
13
|
+
# db.configure
|
|
14
|
+
#
|
|
15
|
+
# @return [ Array<Mongo::DB, Array<Mongo:DB>> ] The Mongo databases.
|
|
16
|
+
#
|
|
17
|
+
# @since 2.0.0.rc.1
|
|
18
|
+
def configure
|
|
19
|
+
[ master.db(name), slaves.map { |slave| slave.db(name) } ]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Create the new db configuration class.
|
|
23
|
+
#
|
|
24
|
+
# @example Initialize the class.
|
|
25
|
+
# Config::Database.new(
|
|
26
|
+
# false, "uri" => { "mongodb://durran:password@localhost:27017/mongoid" }
|
|
27
|
+
# )
|
|
28
|
+
#
|
|
29
|
+
# @param [ Hash ] options The configuration options.
|
|
30
|
+
#
|
|
31
|
+
# @option options [ String ] :database The database name.
|
|
32
|
+
# @option options [ String ] :host The database host.
|
|
33
|
+
# @option options [ String ] :password The password for authentication.
|
|
34
|
+
# @option options [ Integer ] :port The port for the database.
|
|
35
|
+
# @option options [ String ] :uri The uri for the database.
|
|
36
|
+
# @option options [ String ] :username The user for authentication.
|
|
37
|
+
#
|
|
38
|
+
# @since 2.0.0.rc.1
|
|
39
|
+
def initialize(options = {})
|
|
40
|
+
merge!(options)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
# Do we need to authenticate against the database?
|
|
46
|
+
#
|
|
47
|
+
# @example Are we authenticating?
|
|
48
|
+
# db.authenticating?
|
|
49
|
+
#
|
|
50
|
+
# @return [ true, false ] True if auth is needed, false if not.
|
|
51
|
+
#
|
|
52
|
+
# @since 2.0.0.rc.1
|
|
53
|
+
def authenticating?
|
|
54
|
+
username || password
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Takes the supplied options in the hash and created a URI from them to
|
|
58
|
+
# pass to the Mongo connection object.
|
|
59
|
+
#
|
|
60
|
+
# @example Build the URI.
|
|
61
|
+
# db.build_uri
|
|
62
|
+
#
|
|
63
|
+
# @param [ Hash ] options The options to build with.
|
|
64
|
+
#
|
|
65
|
+
# @return [ String ] A mongo compliant URI string.
|
|
66
|
+
#
|
|
67
|
+
# @since 2.0.0.rc.1
|
|
68
|
+
def build_uri(options = {})
|
|
69
|
+
"mongodb://".tap do |base|
|
|
70
|
+
base << "#{username}:#{password}@" if authenticating?
|
|
71
|
+
base << "#{options["host"] || "localhost"}:#{options["port"] || 27017}"
|
|
72
|
+
base << "/#{self["database"]}" if authenticating?
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Create the mongo master connection from either the supplied URI
|
|
77
|
+
# or a generated one, while setting pool size and logging.
|
|
78
|
+
#
|
|
79
|
+
# @example Create the connection.
|
|
80
|
+
# db.connection
|
|
81
|
+
#
|
|
82
|
+
# @return [ Mongo::Connection ] The mongo connection.
|
|
83
|
+
#
|
|
84
|
+
# @since 2.0.0.rc.1
|
|
85
|
+
def master
|
|
86
|
+
Mongo::Connection.from_uri(uri(self), optional).tap do |conn|
|
|
87
|
+
conn.apply_saved_authentication
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Create the mongo slave connections from either the supplied URI
|
|
92
|
+
# or a generated one, while setting pool size and logging.
|
|
93
|
+
#
|
|
94
|
+
# @example Create the connection.
|
|
95
|
+
# db.connection
|
|
96
|
+
#
|
|
97
|
+
# @return [ Array<Mongo::Connection> ] The mongo slave connections.
|
|
98
|
+
#
|
|
99
|
+
# @since 2.0.0.rc.1
|
|
100
|
+
def slaves
|
|
101
|
+
(self["slaves"] || []).map do |options|
|
|
102
|
+
Mongo::Connection.from_uri(uri(options), optional(true)).tap do |conn|
|
|
103
|
+
conn.apply_saved_authentication
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Convenience for accessing the hash via dot notation.
|
|
109
|
+
#
|
|
110
|
+
# @example Access a value in alternate syntax.
|
|
111
|
+
# db.host
|
|
112
|
+
#
|
|
113
|
+
# @return [ Object ] The value in the hash.
|
|
114
|
+
#
|
|
115
|
+
# @since 2.0.0.rc.1
|
|
116
|
+
def method_missing(name, *args, &block)
|
|
117
|
+
self[name.to_s]
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Get the name of the database, from either the URI supplied or the
|
|
121
|
+
# database value in the options.
|
|
122
|
+
#
|
|
123
|
+
# @example Get the database name.
|
|
124
|
+
# db.name
|
|
125
|
+
#
|
|
126
|
+
# @return [ String ] The database name.
|
|
127
|
+
#
|
|
128
|
+
# @since 2.0.0.rc.1
|
|
129
|
+
def name
|
|
130
|
+
db_name = URI.parse(uri(self)).path.to_s.sub("/", "")
|
|
131
|
+
db_name.blank? ? database : db_name
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Get the options used in creating the database connection.
|
|
135
|
+
#
|
|
136
|
+
# @example Get the options.
|
|
137
|
+
# db.options
|
|
138
|
+
#
|
|
139
|
+
# @param [ true, false ] slave Are the options for a slave db?
|
|
140
|
+
#
|
|
141
|
+
# @return [ Hash ] The hash of configuration options.
|
|
142
|
+
#
|
|
143
|
+
# @since 2.0.0.rc.1
|
|
144
|
+
def optional(slave = false)
|
|
145
|
+
{
|
|
146
|
+
:pool_size => pool_size,
|
|
147
|
+
:logger => Mongoid::Logger.new,
|
|
148
|
+
:slave_ok => slave
|
|
149
|
+
}
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Get a Mongo compliant URI for the database connection.
|
|
153
|
+
#
|
|
154
|
+
# @example Get the URI.
|
|
155
|
+
# db.uri
|
|
156
|
+
#
|
|
157
|
+
# @param [ Hash ] options The options hash.
|
|
158
|
+
#
|
|
159
|
+
# @return [ String ] The URI for the connection.
|
|
160
|
+
#
|
|
161
|
+
# @since 2.0.0.rc.1
|
|
162
|
+
def uri(options = {})
|
|
163
|
+
options["uri"] || build_uri(options)
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Config #:nodoc:
|
|
4
|
+
class ReplsetDatabase < Hash
|
|
5
|
+
|
|
6
|
+
# Configure the database connections. This will return an array
|
|
7
|
+
# containing one Mongo::DB and nil (to keep compatibility with Mongoid::Config::Database)
|
|
8
|
+
# If you want the reads to go to a secondary node use the :read_secondary(true): option
|
|
9
|
+
#
|
|
10
|
+
# @example Configure the connection.
|
|
11
|
+
# db.configure
|
|
12
|
+
#
|
|
13
|
+
# @return [ Array<Mongo::DB, nil ] The Mongo databases.
|
|
14
|
+
#
|
|
15
|
+
# @since 2.0.0.rc.5
|
|
16
|
+
def configure
|
|
17
|
+
#yes, construction is weird but the driver wants "A list of host-port pairs ending with a hash containing any options"
|
|
18
|
+
#mongo likes symbols
|
|
19
|
+
options = self.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo}
|
|
20
|
+
connection = Mongo::ReplSetConnection.new(*(self['hosts'] << options))
|
|
21
|
+
[ connection.db(self['database']), nil ]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Create the new db configuration class.
|
|
25
|
+
#
|
|
26
|
+
# @example Initialize the class.
|
|
27
|
+
# Config::ReplsetDatabase.new(
|
|
28
|
+
# "hosts" => [[host1,port1],[host2,port2]]
|
|
29
|
+
# )
|
|
30
|
+
#
|
|
31
|
+
# replSet does not supports auth
|
|
32
|
+
#
|
|
33
|
+
# @param [ Hash ] options The configuration options.
|
|
34
|
+
#
|
|
35
|
+
# @option options [ Array ] :hosts The database host.
|
|
36
|
+
# @option options [ String ] :database The database name.
|
|
37
|
+
# @option options [ Boolean ] :read_secondary Tells the driver to read from secondaries.
|
|
38
|
+
# ...
|
|
39
|
+
#
|
|
40
|
+
# @see Mongo::ReplSetConnection for all options
|
|
41
|
+
#
|
|
42
|
+
# @since 2.0.0.rc.5
|
|
43
|
+
def initialize(options = {})
|
|
44
|
+
merge!(options)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|