mongoid-braxton 2.0.2

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 (226) 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 +41 -0
  5. data/lib/config/locales/de.yml +41 -0
  6. data/lib/config/locales/en.yml +45 -0
  7. data/lib/config/locales/es.yml +41 -0
  8. data/lib/config/locales/fr.yml +42 -0
  9. data/lib/config/locales/hu.yml +44 -0
  10. data/lib/config/locales/id.yml +46 -0
  11. data/lib/config/locales/it.yml +39 -0
  12. data/lib/config/locales/ja.yml +40 -0
  13. data/lib/config/locales/kr.yml +65 -0
  14. data/lib/config/locales/nl.yml +39 -0
  15. data/lib/config/locales/pl.yml +39 -0
  16. data/lib/config/locales/pt-BR.yml +40 -0
  17. data/lib/config/locales/pt.yml +40 -0
  18. data/lib/config/locales/ro.yml +46 -0
  19. data/lib/config/locales/ru.yml +41 -0
  20. data/lib/config/locales/sv.yml +40 -0
  21. data/lib/config/locales/vi.yml +45 -0
  22. data/lib/config/locales/zh-CN.yml +33 -0
  23. data/lib/mongoid.rb +140 -0
  24. data/lib/mongoid/atomicity.rb +111 -0
  25. data/lib/mongoid/attributes.rb +185 -0
  26. data/lib/mongoid/attributes/processing.rb +145 -0
  27. data/lib/mongoid/callbacks.rb +23 -0
  28. data/lib/mongoid/collection.rb +137 -0
  29. data/lib/mongoid/collections.rb +71 -0
  30. data/lib/mongoid/collections/master.rb +37 -0
  31. data/lib/mongoid/collections/operations.rb +42 -0
  32. data/lib/mongoid/collections/retry.rb +39 -0
  33. data/lib/mongoid/components.rb +45 -0
  34. data/lib/mongoid/config.rb +349 -0
  35. data/lib/mongoid/config/database.rb +167 -0
  36. data/lib/mongoid/config/replset_database.rb +78 -0
  37. data/lib/mongoid/contexts.rb +19 -0
  38. data/lib/mongoid/contexts/enumerable.rb +275 -0
  39. data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
  40. data/lib/mongoid/contexts/mongo.rb +345 -0
  41. data/lib/mongoid/copyable.rb +46 -0
  42. data/lib/mongoid/criteria.rb +357 -0
  43. data/lib/mongoid/criterion/builder.rb +34 -0
  44. data/lib/mongoid/criterion/complex.rb +34 -0
  45. data/lib/mongoid/criterion/creational.rb +34 -0
  46. data/lib/mongoid/criterion/exclusion.rb +108 -0
  47. data/lib/mongoid/criterion/inclusion.rb +198 -0
  48. data/lib/mongoid/criterion/inspection.rb +22 -0
  49. data/lib/mongoid/criterion/optional.rb +193 -0
  50. data/lib/mongoid/criterion/selector.rb +143 -0
  51. data/lib/mongoid/criterion/unconvertable.rb +20 -0
  52. data/lib/mongoid/cursor.rb +86 -0
  53. data/lib/mongoid/default_scope.rb +36 -0
  54. data/lib/mongoid/dirty.rb +253 -0
  55. data/lib/mongoid/document.rb +284 -0
  56. data/lib/mongoid/errors.rb +13 -0
  57. data/lib/mongoid/errors/document_not_found.rb +29 -0
  58. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  59. data/lib/mongoid/errors/invalid_database.rb +20 -0
  60. data/lib/mongoid/errors/invalid_field.rb +19 -0
  61. data/lib/mongoid/errors/invalid_options.rb +16 -0
  62. data/lib/mongoid/errors/invalid_type.rb +26 -0
  63. data/lib/mongoid/errors/mixed_relations.rb +37 -0
  64. data/lib/mongoid/errors/mongoid_error.rb +27 -0
  65. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
  66. data/lib/mongoid/errors/unsaved_document.rb +23 -0
  67. data/lib/mongoid/errors/unsupported_version.rb +21 -0
  68. data/lib/mongoid/errors/validations.rb +24 -0
  69. data/lib/mongoid/extensions.rb +123 -0
  70. data/lib/mongoid/extensions/array/conversions.rb +23 -0
  71. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  72. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  73. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  74. data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
  75. data/lib/mongoid/extensions/date/conversions.rb +25 -0
  76. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  77. data/lib/mongoid/extensions/false_class/equality.rb +13 -0
  78. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  79. data/lib/mongoid/extensions/hash/conversions.rb +19 -0
  80. data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
  81. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  82. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  83. data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
  84. data/lib/mongoid/extensions/object/checks.rb +32 -0
  85. data/lib/mongoid/extensions/object/conversions.rb +25 -0
  86. data/lib/mongoid/extensions/object/reflections.rb +17 -0
  87. data/lib/mongoid/extensions/object/yoda.rb +27 -0
  88. data/lib/mongoid/extensions/object_id/conversions.rb +96 -0
  89. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  90. data/lib/mongoid/extensions/range/conversions.rb +25 -0
  91. data/lib/mongoid/extensions/set/conversions.rb +20 -0
  92. data/lib/mongoid/extensions/string/conversions.rb +34 -0
  93. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  94. data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
  95. data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
  96. data/lib/mongoid/extensions/time_conversions.rb +38 -0
  97. data/lib/mongoid/extensions/true_class/equality.rb +13 -0
  98. data/lib/mongoid/extras.rb +42 -0
  99. data/lib/mongoid/factory.rb +37 -0
  100. data/lib/mongoid/field.rb +162 -0
  101. data/lib/mongoid/fields.rb +183 -0
  102. data/lib/mongoid/finders.rb +127 -0
  103. data/lib/mongoid/hierarchy.rb +85 -0
  104. data/lib/mongoid/identity.rb +92 -0
  105. data/lib/mongoid/indexes.rb +38 -0
  106. data/lib/mongoid/inspection.rb +54 -0
  107. data/lib/mongoid/javascript.rb +21 -0
  108. data/lib/mongoid/javascript/functions.yml +37 -0
  109. data/lib/mongoid/json.rb +16 -0
  110. data/lib/mongoid/keys.rb +131 -0
  111. data/lib/mongoid/logger.rb +18 -0
  112. data/lib/mongoid/matchers.rb +32 -0
  113. data/lib/mongoid/matchers/all.rb +11 -0
  114. data/lib/mongoid/matchers/default.rb +70 -0
  115. data/lib/mongoid/matchers/exists.rb +13 -0
  116. data/lib/mongoid/matchers/gt.rb +11 -0
  117. data/lib/mongoid/matchers/gte.rb +11 -0
  118. data/lib/mongoid/matchers/in.rb +11 -0
  119. data/lib/mongoid/matchers/lt.rb +11 -0
  120. data/lib/mongoid/matchers/lte.rb +11 -0
  121. data/lib/mongoid/matchers/ne.rb +11 -0
  122. data/lib/mongoid/matchers/nin.rb +11 -0
  123. data/lib/mongoid/matchers/or.rb +30 -0
  124. data/lib/mongoid/matchers/size.rb +11 -0
  125. data/lib/mongoid/matchers/strategies.rb +63 -0
  126. data/lib/mongoid/multi_database.rb +11 -0
  127. data/lib/mongoid/multi_parameter_attributes.rb +82 -0
  128. data/lib/mongoid/named_scope.rb +137 -0
  129. data/lib/mongoid/nested_attributes.rb +51 -0
  130. data/lib/mongoid/observer.rb +67 -0
  131. data/lib/mongoid/paranoia.rb +103 -0
  132. data/lib/mongoid/paths.rb +61 -0
  133. data/lib/mongoid/persistence.rb +240 -0
  134. data/lib/mongoid/persistence/atomic.rb +88 -0
  135. data/lib/mongoid/persistence/atomic/add_to_set.rb +32 -0
  136. data/lib/mongoid/persistence/atomic/inc.rb +28 -0
  137. data/lib/mongoid/persistence/atomic/operation.rb +44 -0
  138. data/lib/mongoid/persistence/atomic/pull_all.rb +33 -0
  139. data/lib/mongoid/persistence/atomic/push.rb +28 -0
  140. data/lib/mongoid/persistence/command.rb +71 -0
  141. data/lib/mongoid/persistence/insert.rb +53 -0
  142. data/lib/mongoid/persistence/insert_embedded.rb +43 -0
  143. data/lib/mongoid/persistence/remove.rb +44 -0
  144. data/lib/mongoid/persistence/remove_all.rb +40 -0
  145. data/lib/mongoid/persistence/remove_embedded.rb +48 -0
  146. data/lib/mongoid/persistence/update.rb +77 -0
  147. data/lib/mongoid/railtie.rb +139 -0
  148. data/lib/mongoid/railties/database.rake +171 -0
  149. data/lib/mongoid/railties/document.rb +12 -0
  150. data/lib/mongoid/relations.rb +107 -0
  151. data/lib/mongoid/relations/accessors.rb +175 -0
  152. data/lib/mongoid/relations/auto_save.rb +34 -0
  153. data/lib/mongoid/relations/binding.rb +26 -0
  154. data/lib/mongoid/relations/bindings.rb +9 -0
  155. data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
  156. data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
  157. data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
  158. data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
  159. data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
  160. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +103 -0
  161. data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
  162. data/lib/mongoid/relations/builder.rb +42 -0
  163. data/lib/mongoid/relations/builders.rb +79 -0
  164. data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
  165. data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
  166. data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
  167. data/lib/mongoid/relations/builders/nested_attributes/many.rb +126 -0
  168. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  169. data/lib/mongoid/relations/builders/referenced/in.rb +29 -0
  170. data/lib/mongoid/relations/builders/referenced/many.rb +47 -0
  171. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
  172. data/lib/mongoid/relations/builders/referenced/one.rb +27 -0
  173. data/lib/mongoid/relations/cascading.rb +55 -0
  174. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  175. data/lib/mongoid/relations/cascading/destroy.rb +19 -0
  176. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  177. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  178. data/lib/mongoid/relations/constraint.rb +42 -0
  179. data/lib/mongoid/relations/cyclic.rb +103 -0
  180. data/lib/mongoid/relations/embedded/atomic.rb +86 -0
  181. data/lib/mongoid/relations/embedded/atomic/operation.rb +63 -0
  182. data/lib/mongoid/relations/embedded/atomic/pull.rb +65 -0
  183. data/lib/mongoid/relations/embedded/atomic/push_all.rb +59 -0
  184. data/lib/mongoid/relations/embedded/atomic/set.rb +61 -0
  185. data/lib/mongoid/relations/embedded/atomic/unset.rb +41 -0
  186. data/lib/mongoid/relations/embedded/in.rb +173 -0
  187. data/lib/mongoid/relations/embedded/many.rb +499 -0
  188. data/lib/mongoid/relations/embedded/one.rb +170 -0
  189. data/lib/mongoid/relations/macros.rb +310 -0
  190. data/lib/mongoid/relations/many.rb +215 -0
  191. data/lib/mongoid/relations/metadata.rb +539 -0
  192. data/lib/mongoid/relations/nested_builder.rb +68 -0
  193. data/lib/mongoid/relations/one.rb +47 -0
  194. data/lib/mongoid/relations/polymorphic.rb +54 -0
  195. data/lib/mongoid/relations/proxy.rb +143 -0
  196. data/lib/mongoid/relations/referenced/batch.rb +71 -0
  197. data/lib/mongoid/relations/referenced/batch/insert.rb +57 -0
  198. data/lib/mongoid/relations/referenced/in.rb +216 -0
  199. data/lib/mongoid/relations/referenced/many.rb +516 -0
  200. data/lib/mongoid/relations/referenced/many_to_many.rb +396 -0
  201. data/lib/mongoid/relations/referenced/one.rb +222 -0
  202. data/lib/mongoid/relations/reflections.rb +45 -0
  203. data/lib/mongoid/safe.rb +23 -0
  204. data/lib/mongoid/safety.rb +207 -0
  205. data/lib/mongoid/scope.rb +31 -0
  206. data/lib/mongoid/serialization.rb +99 -0
  207. data/lib/mongoid/sharding.rb +51 -0
  208. data/lib/mongoid/state.rb +67 -0
  209. data/lib/mongoid/timestamps.rb +14 -0
  210. data/lib/mongoid/timestamps/created.rb +31 -0
  211. data/lib/mongoid/timestamps/updated.rb +33 -0
  212. data/lib/mongoid/validations.rb +124 -0
  213. data/lib/mongoid/validations/associated.rb +44 -0
  214. data/lib/mongoid/validations/referenced.rb +58 -0
  215. data/lib/mongoid/validations/uniqueness.rb +85 -0
  216. data/lib/mongoid/version.rb +4 -0
  217. data/lib/mongoid/versioning.rb +113 -0
  218. data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
  219. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +20 -0
  220. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  221. data/lib/rails/generators/mongoid/model/templates/model.rb +19 -0
  222. data/lib/rails/generators/mongoid/observer/observer_generator.rb +17 -0
  223. data/lib/rails/generators/mongoid/observer/templates/observer.rb +4 -0
  224. data/lib/rails/generators/mongoid_generator.rb +70 -0
  225. data/lib/rails/mongoid.rb +58 -0
  226. metadata +406 -0
@@ -0,0 +1,145 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Attributes #:nodoc:
4
+
5
+ # This module contains the behavior for processing attributes.
6
+ module Processing
7
+
8
+ # Process the provided attributes casting them to their proper values if a
9
+ # field exists for them on the document. This will be limited to only the
10
+ # attributes provided in the suppied +Hash+ so that no extra nil values get
11
+ # put into the document's attributes.
12
+ #
13
+ # @example Process the attributes.
14
+ # person.process(:title => "sir", :age => 40)
15
+ #
16
+ # @param [ Hash ] attrs The attributes to set.
17
+ # @param [ Boolean ] guard_protected_attributes False to skip mass assignment protection.
18
+ #
19
+ # @since 2.0.0.rc.7
20
+ def process(attrs = nil, guard_protected_attributes = true)
21
+ attrs ||= {}
22
+ attrs = sanitize_for_mass_assignment(attrs) if guard_protected_attributes
23
+ attrs.each_pair do |key, value|
24
+ next if pending_attribute?(key, value)
25
+ process_attribute(key, value)
26
+ end
27
+ yield self if block_given?
28
+ process_pending and setup_modifications
29
+ end
30
+
31
+ protected
32
+
33
+ # If the key provided a relation or a nested attribute, where we have to
34
+ # hold off on the setting of the attribute until everything else has been
35
+ # set?
36
+ #
37
+ # @example Is the attribute pending?
38
+ # document.pending_attribute?(:name, "Durran")
39
+ #
40
+ # @param [ Synbol ] key The name of the attribute.
41
+ # @param [ Object ] value The value of the attribute.
42
+ #
43
+ # @return [ true, false ] True if pending, false if not.
44
+ #
45
+ # @since 2.0.0.rc.7
46
+ def pending_attribute?(key, value)
47
+ name = key.to_s
48
+ if relations.has_key?(name)
49
+ pending_relations[name] = value
50
+ return true
51
+ end
52
+ if nested_attributes.include?("#{name}=")
53
+ pending_nested[name] = value
54
+ return true
55
+ end
56
+ return false
57
+ end
58
+
59
+ # Get all the pending relations that need to be set.
60
+ #
61
+ # @example Get the pending relations.
62
+ # document.pending_relations
63
+ #
64
+ # @return [ Hash ] The pending relations in key/value pairs.
65
+ #
66
+ # @since 2.0.0.rc.7
67
+ def pending_relations
68
+ @pending_relations ||= {}
69
+ end
70
+
71
+ # Get all the pending nested attributes that need to be set.
72
+ #
73
+ # @example Get the pending nested attributes.
74
+ # document.pending_nested
75
+ #
76
+ # @return [ Hash ] The pending nested attributes in key/value pairs.
77
+ #
78
+ # @since 2.0.0.rc.7
79
+ def pending_nested
80
+ @pending_nested ||= {}
81
+ end
82
+
83
+ # If the attribute is dynamic, add a field for it with a type of object
84
+ # and then either way set the value.
85
+ #
86
+ # @example Process the attribute.
87
+ # document.process_attribute(name, value)
88
+ #
89
+ # @param [ Symbol ] name The name of the field.
90
+ # @param [ Object ] value The value of the field.
91
+ #
92
+ # @since 2.0.0.rc.7
93
+ def process_attribute(name, value)
94
+ if Mongoid.allow_dynamic_fields && !respond_to?("#{name}=")
95
+ write_attribute(name, value)
96
+ else
97
+ send("#{name}=", value)
98
+ end
99
+ end
100
+
101
+ # Process all the pending nested attributes that needed to wait until
102
+ # ids were set to fire off.
103
+ #
104
+ # @example Process the nested attributes.
105
+ # document.process_nested
106
+ #
107
+ # @since 2.0.0.rc.7
108
+ def process_nested
109
+ pending_nested.each_pair do |name, value|
110
+ send("#{name}=", value)
111
+ end
112
+ end
113
+
114
+ # Process all the pending items, then clear them out.
115
+ #
116
+ # @example Process the pending items.
117
+ # document.process_pending
118
+ #
119
+ # @since 2.0.0.rc.7
120
+ def process_pending
121
+ process_nested and process_relations
122
+ pending_nested.clear and pending_relations.clear
123
+ end
124
+
125
+ # Process all the pending relations that needed to wait until ids were set
126
+ # to fire off.
127
+ #
128
+ # @example Process the relations.
129
+ # document.process_relations
130
+ #
131
+ # @since 2.0.0.rc.7
132
+ def process_relations
133
+ pending_relations.each_pair do |name, value|
134
+ metadata = relations[name]
135
+ if value.is_a?(Hash)
136
+ metadata.nested_builder(value, {}).build(self)
137
+ else
138
+ send("#{name}=", value, :binding => true)
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Callbacks
4
+ extend ActiveSupport::Concern
5
+
6
+ CALLBACKS = [
7
+ :before_validation, :after_validation,
8
+ :after_initialize,
9
+ :before_create, :around_create, :after_create,
10
+ :before_destroy, :around_destroy, :after_destroy,
11
+ :before_save, :around_save, :after_save,
12
+ :before_update, :around_update, :after_update,
13
+ ]
14
+
15
+ included do
16
+ extend ActiveModel::Callbacks
17
+ include ActiveModel::Validations::Callbacks
18
+
19
+ define_model_callbacks :initialize, :only => :after
20
+ define_model_callbacks :create, :destroy, :save, :update
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,137 @@
1
+ # encoding: utf-8
2
+ require "mongoid/collections/retry"
3
+ require "mongoid/collections/operations"
4
+ require "mongoid/collections/master"
5
+
6
+ module Mongoid #:nodoc
7
+
8
+ # This class is the Mongoid wrapper to the Mongo Ruby driver's collection
9
+ # object.
10
+ class Collection
11
+ attr_reader :counter, :name
12
+
13
+ # All write operations should delegate to the master connection. These
14
+ # operations mimic the methods on a Mongo:Collection.
15
+ #
16
+ # @example Delegate the operation.
17
+ # collection.save({ :name => "Al" })
18
+ Collections::Operations::PROXIED.each do |name|
19
+ define_method(name) { |*args| master.send(name, *args) }
20
+ end
21
+
22
+ # Find documents from the database given a selector and options.
23
+ #
24
+ # @example Find documents in the collection.
25
+ # collection.find({ :test => "value" })
26
+ #
27
+ # @param [ Hash ] selector The query selector.
28
+ # @param [ Hash ] options The options to pass to the db.
29
+ #
30
+ # @return [ Cursor ] The results.
31
+ def find(selector = {}, options = {})
32
+ cursor = Mongoid::Cursor.new(@klass, self, master(options).find(selector, options))
33
+ if block_given?
34
+ yield cursor; cursor.close
35
+ else
36
+ cursor
37
+ end
38
+ end
39
+
40
+ # Find the first document from the database given a selector and options.
41
+ #
42
+ # @example Find one document.
43
+ # collection.find_one({ :test => "value" })
44
+ #
45
+ # @param [ Hash ] selector The query selector.
46
+ # @param [ Hash ] options The options to pass to the db.
47
+ #
48
+ # @return [ Document, nil ] A matching document or nil if none found.
49
+ def find_one(selector = {}, options = {})
50
+ master(options).find_one(selector, options)
51
+ end
52
+
53
+ # Initialize a new Mongoid::Collection, setting up the master, slave, and
54
+ # name attributes. Masters will be used for writes, slaves for reads.
55
+ #
56
+ # @example Create the new collection.
57
+ # Collection.new(masters, slaves, "test")
58
+ #
59
+ # @param [ Class ] klass The class the collection is for.
60
+ # @param [ String ] name The name of the collection.
61
+ def initialize(klass, name)
62
+ @klass, @name = klass, name
63
+ end
64
+
65
+ # Inserts one or more documents in the collection.
66
+ #
67
+ # @example Insert documents.
68
+ # collection.insert(
69
+ # { "field" => "value" },
70
+ # :safe => true
71
+ # )
72
+ #
73
+ # @param [ Hash, Array<Hash> ] documents A single document or multiples.
74
+ # @param [ Hash ] options The options.
75
+ #
76
+ # @since 2.0.2, batch-relational-insert
77
+ def insert(documents, options = {})
78
+ inserter = Thread.current[:mongoid_batch_insert]
79
+ if inserter
80
+ inserter.consume(documents, options)
81
+ else
82
+ master(options).insert(documents, options)
83
+ end
84
+ end
85
+
86
+ # Perform a map/reduce on the documents.
87
+ #
88
+ # @example Perform the map/reduce.
89
+ # collection.map_reduce(map, reduce)
90
+ #
91
+ # @param [ String ] map The map javascript function.
92
+ # @param [ String ] reduce The reduce javascript function.
93
+ # @param [ Hash ] options The options to pass to the db.
94
+ #
95
+ # @return [ Cursor ] The results.
96
+ def map_reduce(map, reduce, options = {})
97
+ master(options).map_reduce(map, reduce, options)
98
+ end
99
+ alias :mapreduce :map_reduce
100
+
101
+ # Return the object responsible for writes to the database. This will
102
+ # always return a collection associated with the Master DB.
103
+ #
104
+ # @example Get the master connection.
105
+ # collection.master
106
+ #
107
+ # @return [ Master ] The master connection.
108
+ def master(options = {})
109
+ options.delete(:cache)
110
+ db = Mongoid.databases[@klass.database] || Mongoid.master
111
+ @master ||= Collections::Master.new(db, @name)
112
+ end
113
+
114
+ # Updates one or more documents in the collection.
115
+ #
116
+ # @example Update documents.
117
+ # collection.update(
118
+ # { "_id" => BSON::OjectId.new },
119
+ # { "$push" => { "addresses" => { "_id" => "street" } } },
120
+ # :safe => true
121
+ # )
122
+ #
123
+ # @param [ Hash ] selector The document selector.
124
+ # @param [ Hash ] document The modifier.
125
+ # @param [ Hash ] options The options.
126
+ #
127
+ # @since 2.0.0
128
+ def update(selector, document, options = {})
129
+ updater = Thread.current[:mongoid_atomic_update]
130
+ if updater
131
+ updater.consume(selector, document, options)
132
+ else
133
+ master(options).update(selector, document, options)
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,71 @@
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
+ raise Errors::InvalidCollection.new(self) if embedded? && !cyclic
22
+ self._collection || set_collection
23
+ add_indexes; self._collection
24
+ end
25
+
26
+ # Return the database associated with this collection.
27
+ #
28
+ # Example:
29
+ #
30
+ # <tt>Person.db</tt>
31
+ def db
32
+ collection.db
33
+ end
34
+
35
+ # Convenience method for getting index information from the collection.
36
+ #
37
+ # Example:
38
+ #
39
+ # <tt>Person.index_information</tt>
40
+ def index_information
41
+ collection.index_information
42
+ end
43
+
44
+ # The MongoDB logger is not exposed through the driver to be changed
45
+ # after initialization of the connection, this is a hacky way around that
46
+ # if logging needs to be changed at runtime.
47
+ #
48
+ # Example:
49
+ #
50
+ # <tt>Person.logger = Logger.new($stdout)</tt>
51
+ def logger=(logger)
52
+ db.connection.instance_variable_set(:@logger, logger)
53
+ end
54
+
55
+ # Macro for setting the collection name to store in.
56
+ #
57
+ # Example:
58
+ #
59
+ # <tt>Person.store_in :population</tt>
60
+ def store_in(name)
61
+ self.collection_name = name.to_s
62
+ set_collection
63
+ end
64
+
65
+ protected
66
+ def set_collection
67
+ self._collection = Mongoid::Collection.new(self, self.collection_name)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Collections #:nodoc:
4
+
5
+ # This class wraps the MongoDB master database.
6
+ class Master
7
+ include Mongoid::Collections::Retry
8
+
9
+ attr_reader :collection
10
+
11
+ # All read and write operations should delegate to the master connection.
12
+ # These operations mimic the methods on a Mongo:Collection.
13
+ #
14
+ # @example Proxy the driver save.
15
+ # collection.save({ :name => "Al" })
16
+ Operations::ALL.each do |name|
17
+ define_method(name) do |*args|
18
+ retry_on_connection_failure do
19
+ collection.send(name, *args)
20
+ end
21
+ end
22
+ end
23
+
24
+ # Create the new database writer. Will create a collection from the
25
+ # master database.
26
+ #
27
+ # @example Create a new wrapped master.
28
+ # Master.new(db, "testing")
29
+ #
30
+ # @param [ Mongo::DB ] master The master database.
31
+ # @param [ String ] name The name of the database.
32
+ def initialize(master, name)
33
+ @collection = master.collection(name)
34
+ end
35
+ end
36
+ end
37
+ 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, :update ]
40
+ end
41
+ end
42
+ end