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.
Files changed (199) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +50 -0
  3. data/Rakefile +51 -0
  4. data/lib/config/locales/bg.yml +44 -0
  5. data/lib/config/locales/de.yml +44 -0
  6. data/lib/config/locales/en.yml +45 -0
  7. data/lib/config/locales/es.yml +44 -0
  8. data/lib/config/locales/fr.yml +45 -0
  9. data/lib/config/locales/hu.yml +47 -0
  10. data/lib/config/locales/it.yml +42 -0
  11. data/lib/config/locales/kr.yml +68 -0
  12. data/lib/config/locales/nl.yml +42 -0
  13. data/lib/config/locales/pl.yml +42 -0
  14. data/lib/config/locales/pt-br.yml +43 -0
  15. data/lib/config/locales/pt.yml +43 -0
  16. data/lib/config/locales/ro.yml +49 -0
  17. data/lib/config/locales/sv.yml +43 -0
  18. data/lib/config/locales/zh-CN.yml +34 -0
  19. data/lib/mongoid/atomicity.rb +111 -0
  20. data/lib/mongoid/attributes.rb +251 -0
  21. data/lib/mongoid/callbacks.rb +13 -0
  22. data/lib/mongoid/collection.rb +137 -0
  23. data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
  24. data/lib/mongoid/collections/master.rb +29 -0
  25. data/lib/mongoid/collections/operations.rb +42 -0
  26. data/lib/mongoid/collections/slaves.rb +45 -0
  27. data/lib/mongoid/collections.rb +70 -0
  28. data/lib/mongoid/components.rb +45 -0
  29. data/lib/mongoid/config/database.rb +167 -0
  30. data/lib/mongoid/config/replset_database.rb +48 -0
  31. data/lib/mongoid/config.rb +343 -0
  32. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  33. data/lib/mongoid/contexts/enumerable.rb +226 -0
  34. data/lib/mongoid/contexts/ids.rb +25 -0
  35. data/lib/mongoid/contexts/mongo.rb +345 -0
  36. data/lib/mongoid/contexts/paging.rb +50 -0
  37. data/lib/mongoid/contexts.rb +21 -0
  38. data/lib/mongoid/copyable.rb +44 -0
  39. data/lib/mongoid/criteria.rb +325 -0
  40. data/lib/mongoid/criterion/complex.rb +34 -0
  41. data/lib/mongoid/criterion/creational.rb +34 -0
  42. data/lib/mongoid/criterion/exclusion.rb +67 -0
  43. data/lib/mongoid/criterion/inclusion.rb +134 -0
  44. data/lib/mongoid/criterion/inspection.rb +20 -0
  45. data/lib/mongoid/criterion/optional.rb +213 -0
  46. data/lib/mongoid/criterion/selector.rb +74 -0
  47. data/lib/mongoid/cursor.rb +81 -0
  48. data/lib/mongoid/default_scope.rb +28 -0
  49. data/lib/mongoid/dirty.rb +251 -0
  50. data/lib/mongoid/document.rb +256 -0
  51. data/lib/mongoid/errors/document_not_found.rb +29 -0
  52. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  53. data/lib/mongoid/errors/invalid_database.rb +20 -0
  54. data/lib/mongoid/errors/invalid_field.rb +19 -0
  55. data/lib/mongoid/errors/invalid_options.rb +16 -0
  56. data/lib/mongoid/errors/invalid_type.rb +26 -0
  57. data/lib/mongoid/errors/mongoid_error.rb +27 -0
  58. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
  59. data/lib/mongoid/errors/unsaved_document.rb +23 -0
  60. data/lib/mongoid/errors/unsupported_version.rb +21 -0
  61. data/lib/mongoid/errors/validations.rb +24 -0
  62. data/lib/mongoid/errors.rb +12 -0
  63. data/lib/mongoid/extensions/array/conversions.rb +23 -0
  64. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  65. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  66. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  67. data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
  68. data/lib/mongoid/extensions/date/conversions.rb +25 -0
  69. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  70. data/lib/mongoid/extensions/false_class/equality.rb +13 -0
  71. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  72. data/lib/mongoid/extensions/hash/conversions.rb +19 -0
  73. data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
  74. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  75. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  76. data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
  77. data/lib/mongoid/extensions/object/conversions.rb +25 -0
  78. data/lib/mongoid/extensions/object/reflections.rb +17 -0
  79. data/lib/mongoid/extensions/object/yoda.rb +27 -0
  80. data/lib/mongoid/extensions/object_id/conversions.rb +57 -0
  81. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  82. data/lib/mongoid/extensions/set/conversions.rb +20 -0
  83. data/lib/mongoid/extensions/string/conversions.rb +34 -0
  84. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  85. data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
  86. data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
  87. data/lib/mongoid/extensions/time_conversions.rb +38 -0
  88. data/lib/mongoid/extensions/true_class/equality.rb +13 -0
  89. data/lib/mongoid/extensions.rb +116 -0
  90. data/lib/mongoid/extras.rb +61 -0
  91. data/lib/mongoid/factory.rb +20 -0
  92. data/lib/mongoid/field.rb +95 -0
  93. data/lib/mongoid/fields.rb +138 -0
  94. data/lib/mongoid/finders.rb +173 -0
  95. data/lib/mongoid/hierarchy.rb +85 -0
  96. data/lib/mongoid/identity.rb +89 -0
  97. data/lib/mongoid/indexes.rb +38 -0
  98. data/lib/mongoid/inspection.rb +58 -0
  99. data/lib/mongoid/javascript/functions.yml +37 -0
  100. data/lib/mongoid/javascript.rb +21 -0
  101. data/lib/mongoid/json.rb +16 -0
  102. data/lib/mongoid/keys.rb +77 -0
  103. data/lib/mongoid/logger.rb +18 -0
  104. data/lib/mongoid/matchers/all.rb +11 -0
  105. data/lib/mongoid/matchers/default.rb +27 -0
  106. data/lib/mongoid/matchers/exists.rb +13 -0
  107. data/lib/mongoid/matchers/gt.rb +11 -0
  108. data/lib/mongoid/matchers/gte.rb +11 -0
  109. data/lib/mongoid/matchers/in.rb +11 -0
  110. data/lib/mongoid/matchers/lt.rb +11 -0
  111. data/lib/mongoid/matchers/lte.rb +11 -0
  112. data/lib/mongoid/matchers/ne.rb +11 -0
  113. data/lib/mongoid/matchers/nin.rb +11 -0
  114. data/lib/mongoid/matchers/size.rb +11 -0
  115. data/lib/mongoid/matchers.rb +55 -0
  116. data/lib/mongoid/modifiers/command.rb +18 -0
  117. data/lib/mongoid/modifiers/inc.rb +24 -0
  118. data/lib/mongoid/modifiers.rb +24 -0
  119. data/lib/mongoid/multi_database.rb +11 -0
  120. data/lib/mongoid/multi_parameter_attributes.rb +80 -0
  121. data/lib/mongoid/named_scope.rb +36 -0
  122. data/lib/mongoid/nested_attributes.rb +43 -0
  123. data/lib/mongoid/paranoia.rb +103 -0
  124. data/lib/mongoid/paths.rb +61 -0
  125. data/lib/mongoid/persistence/command.rb +59 -0
  126. data/lib/mongoid/persistence/insert.rb +53 -0
  127. data/lib/mongoid/persistence/insert_embedded.rb +42 -0
  128. data/lib/mongoid/persistence/remove.rb +44 -0
  129. data/lib/mongoid/persistence/remove_all.rb +40 -0
  130. data/lib/mongoid/persistence/remove_embedded.rb +48 -0
  131. data/lib/mongoid/persistence/update.rb +76 -0
  132. data/lib/mongoid/persistence.rb +237 -0
  133. data/lib/mongoid/railtie.rb +129 -0
  134. data/lib/mongoid/railties/database.rake +171 -0
  135. data/lib/mongoid/railties/document.rb +12 -0
  136. data/lib/mongoid/relations/accessors.rb +157 -0
  137. data/lib/mongoid/relations/auto_save.rb +34 -0
  138. data/lib/mongoid/relations/binding.rb +26 -0
  139. data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
  140. data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
  141. data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
  142. data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
  143. data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
  144. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +99 -0
  145. data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
  146. data/lib/mongoid/relations/bindings.rb +9 -0
  147. data/lib/mongoid/relations/builder.rb +42 -0
  148. data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
  149. data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
  150. data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
  151. data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
  152. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  153. data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
  154. data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
  155. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
  156. data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
  157. data/lib/mongoid/relations/builders.rb +79 -0
  158. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  159. data/lib/mongoid/relations/cascading/destroy.rb +19 -0
  160. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  161. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  162. data/lib/mongoid/relations/cascading.rb +55 -0
  163. data/lib/mongoid/relations/constraint.rb +45 -0
  164. data/lib/mongoid/relations/cyclic.rb +97 -0
  165. data/lib/mongoid/relations/embedded/in.rb +173 -0
  166. data/lib/mongoid/relations/embedded/many.rb +483 -0
  167. data/lib/mongoid/relations/embedded/one.rb +170 -0
  168. data/lib/mongoid/relations/macros.rb +306 -0
  169. data/lib/mongoid/relations/many.rb +171 -0
  170. data/lib/mongoid/relations/metadata.rb +533 -0
  171. data/lib/mongoid/relations/nested_builder.rb +68 -0
  172. data/lib/mongoid/relations/one.rb +47 -0
  173. data/lib/mongoid/relations/polymorphic.rb +54 -0
  174. data/lib/mongoid/relations/proxy.rb +128 -0
  175. data/lib/mongoid/relations/referenced/in.rb +216 -0
  176. data/lib/mongoid/relations/referenced/many.rb +443 -0
  177. data/lib/mongoid/relations/referenced/many_to_many.rb +344 -0
  178. data/lib/mongoid/relations/referenced/one.rb +206 -0
  179. data/lib/mongoid/relations/reflections.rb +45 -0
  180. data/lib/mongoid/relations.rb +105 -0
  181. data/lib/mongoid/safe.rb +23 -0
  182. data/lib/mongoid/safety.rb +207 -0
  183. data/lib/mongoid/scope.rb +31 -0
  184. data/lib/mongoid/serialization.rb +99 -0
  185. data/lib/mongoid/state.rb +66 -0
  186. data/lib/mongoid/timestamps.rb +38 -0
  187. data/lib/mongoid/validations/associated.rb +42 -0
  188. data/lib/mongoid/validations/uniqueness.rb +85 -0
  189. data/lib/mongoid/validations.rb +117 -0
  190. data/lib/mongoid/version.rb +4 -0
  191. data/lib/mongoid/versioning.rb +51 -0
  192. data/lib/mongoid.rb +139 -0
  193. data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
  194. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +23 -0
  195. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  196. data/lib/rails/generators/mongoid/model/templates/model.rb +17 -0
  197. data/lib/rails/generators/mongoid_generator.rb +61 -0
  198. data/lib/rails/mongoid.rb +57 -0
  199. 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