mongoid 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (240) hide show
  1. data/README.rdoc +3 -1
  2. data/Rakefile +3 -2
  3. data/lib/config/locales/bg.yml +6 -0
  4. data/lib/config/locales/de.yml +6 -0
  5. data/lib/config/locales/en-GB.yml +48 -0
  6. data/lib/config/locales/en.yml +6 -3
  7. data/lib/config/locales/es.yml +6 -0
  8. data/lib/config/locales/fr.yml +6 -0
  9. data/lib/config/locales/hi.yml +39 -0
  10. data/lib/config/locales/hu.yml +13 -7
  11. data/lib/config/locales/id.yml +3 -0
  12. data/lib/config/locales/it.yml +7 -1
  13. data/lib/config/locales/ja.yml +4 -1
  14. data/lib/config/locales/kr.yml +9 -34
  15. data/lib/config/locales/nl.yml +6 -0
  16. data/lib/config/locales/pl.yml +6 -0
  17. data/lib/config/locales/pt-BR.yml +6 -0
  18. data/lib/config/locales/pt.yml +6 -0
  19. data/lib/config/locales/ro.yml +6 -0
  20. data/lib/config/locales/ru.yml +6 -0
  21. data/lib/config/locales/sv.yml +6 -0
  22. data/lib/config/locales/vi.yml +3 -0
  23. data/lib/config/locales/zh-CN.yml +6 -0
  24. data/lib/mongoid.rb +51 -45
  25. data/lib/mongoid/atomic.rb +145 -0
  26. data/lib/mongoid/atomic/modifiers.rb +109 -0
  27. data/lib/mongoid/atomic/paths.rb +3 -0
  28. data/lib/mongoid/atomic/paths/embedded.rb +43 -0
  29. data/lib/mongoid/atomic/paths/embedded/many.rb +44 -0
  30. data/lib/mongoid/atomic/paths/embedded/one.rb +43 -0
  31. data/lib/mongoid/atomic/paths/root.rb +40 -0
  32. data/lib/mongoid/attributes.rb +12 -23
  33. data/lib/mongoid/attributes/processing.rb +5 -5
  34. data/lib/mongoid/callbacks.rb +2 -0
  35. data/lib/mongoid/collection.rb +12 -59
  36. data/lib/mongoid/collections.rb +23 -20
  37. data/lib/mongoid/collections/master.rb +6 -4
  38. data/lib/mongoid/collections/operations.rb +1 -0
  39. data/lib/mongoid/collections/retry.rb +7 -0
  40. data/lib/mongoid/components.rb +2 -2
  41. data/lib/mongoid/config.rb +42 -55
  42. data/lib/mongoid/config/database.rb +6 -2
  43. data/lib/mongoid/config/replset_database.rb +7 -3
  44. data/lib/mongoid/contexts.rb +9 -3
  45. data/lib/mongoid/contexts/enumerable.rb +7 -3
  46. data/lib/mongoid/contexts/mongo.rb +139 -101
  47. data/lib/mongoid/criteria.rb +86 -69
  48. data/lib/mongoid/criterion/complex.rb +32 -5
  49. data/lib/mongoid/criterion/inclusion.rb +4 -2
  50. data/lib/mongoid/criterion/optional.rb +111 -86
  51. data/lib/mongoid/criterion/selector.rb +8 -4
  52. data/lib/mongoid/cursor.rb +27 -27
  53. data/lib/mongoid/dirty.rb +54 -214
  54. data/lib/mongoid/document.rb +37 -39
  55. data/lib/mongoid/errors/document_not_found.rb +3 -4
  56. data/lib/mongoid/errors/invalid_collection.rb +2 -3
  57. data/lib/mongoid/errors/invalid_database.rb +2 -3
  58. data/lib/mongoid/errors/invalid_field.rb +2 -3
  59. data/lib/mongoid/errors/invalid_options.rb +19 -7
  60. data/lib/mongoid/errors/invalid_type.rb +2 -3
  61. data/lib/mongoid/errors/mongoid_error.rb +5 -6
  62. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +2 -3
  63. data/lib/mongoid/errors/unsupported_version.rb +2 -3
  64. data/lib/mongoid/errors/validations.rb +2 -3
  65. data/lib/mongoid/extensions.rb +8 -62
  66. data/lib/mongoid/extensions/array/deletion.rb +29 -0
  67. data/lib/mongoid/extensions/false_class/equality.rb +14 -1
  68. data/lib/mongoid/extensions/hash/criteria_helpers.rb +21 -10
  69. data/lib/mongoid/extensions/hash/scoping.rb +14 -1
  70. data/lib/mongoid/extensions/nil/collectionization.rb +12 -1
  71. data/lib/mongoid/extensions/object/reflections.rb +33 -2
  72. data/lib/mongoid/extensions/object_id/conversions.rb +2 -36
  73. data/lib/mongoid/extensions/proc/scoping.rb +14 -1
  74. data/lib/mongoid/extensions/string/conversions.rb +4 -16
  75. data/lib/mongoid/extensions/string/inflections.rb +35 -14
  76. data/lib/mongoid/extensions/symbol/inflections.rb +38 -12
  77. data/lib/mongoid/extensions/true_class/equality.rb +14 -1
  78. data/lib/mongoid/extras.rb +11 -30
  79. data/lib/mongoid/factory.rb +1 -1
  80. data/lib/mongoid/fields.rb +121 -29
  81. data/lib/mongoid/fields/mappings.rb +36 -0
  82. data/lib/mongoid/fields/serializable.rb +131 -0
  83. data/lib/mongoid/fields/serializable/array.rb +64 -0
  84. data/lib/mongoid/fields/serializable/big_decimal.rb +42 -0
  85. data/lib/mongoid/fields/serializable/bignum.rb +10 -0
  86. data/lib/mongoid/fields/serializable/binary.rb +11 -0
  87. data/lib/mongoid/fields/serializable/boolean.rb +44 -0
  88. data/lib/mongoid/fields/serializable/date.rb +51 -0
  89. data/lib/mongoid/fields/serializable/date_time.rb +28 -0
  90. data/lib/mongoid/fields/serializable/fixnum.rb +10 -0
  91. data/lib/mongoid/fields/serializable/float.rb +33 -0
  92. data/lib/mongoid/fields/serializable/foreign_keys/array.rb +56 -0
  93. data/lib/mongoid/fields/serializable/foreign_keys/object.rb +43 -0
  94. data/lib/mongoid/fields/serializable/hash.rb +25 -0
  95. data/lib/mongoid/fields/serializable/integer.rb +33 -0
  96. data/lib/mongoid/fields/serializable/object.rb +11 -0
  97. data/lib/mongoid/fields/serializable/object_id.rb +32 -0
  98. data/lib/mongoid/fields/serializable/range.rb +42 -0
  99. data/lib/mongoid/fields/serializable/set.rb +42 -0
  100. data/lib/mongoid/fields/serializable/string.rb +28 -0
  101. data/lib/mongoid/fields/serializable/symbol.rb +28 -0
  102. data/lib/mongoid/fields/serializable/time.rb +12 -0
  103. data/lib/mongoid/fields/serializable/time_with_zone.rb +12 -0
  104. data/lib/mongoid/fields/serializable/timekeeping.rb +102 -0
  105. data/lib/mongoid/finders.rb +61 -37
  106. data/lib/mongoid/hierarchy.rb +43 -8
  107. data/lib/mongoid/identity_map.rb +106 -0
  108. data/lib/mongoid/indexes.rb +17 -1
  109. data/lib/mongoid/javascript.rb +2 -3
  110. data/lib/mongoid/keys.rb +10 -21
  111. data/lib/mongoid/logger.rb +22 -1
  112. data/lib/mongoid/matchers/all.rb +10 -0
  113. data/lib/mongoid/matchers/default.rb +1 -1
  114. data/lib/mongoid/matchers/exists.rb +10 -0
  115. data/lib/mongoid/matchers/gt.rb +10 -0
  116. data/lib/mongoid/matchers/gte.rb +10 -0
  117. data/lib/mongoid/matchers/in.rb +10 -0
  118. data/lib/mongoid/matchers/lt.rb +10 -0
  119. data/lib/mongoid/matchers/lte.rb +10 -0
  120. data/lib/mongoid/matchers/ne.rb +10 -0
  121. data/lib/mongoid/matchers/nin.rb +10 -0
  122. data/lib/mongoid/matchers/or.rb +7 -4
  123. data/lib/mongoid/matchers/size.rb +10 -0
  124. data/lib/mongoid/multi_database.rb +26 -6
  125. data/lib/mongoid/multi_parameter_attributes.rb +40 -17
  126. data/lib/mongoid/named_scope.rb +1 -2
  127. data/lib/mongoid/nested_attributes.rb +4 -1
  128. data/lib/mongoid/observer.rb +108 -5
  129. data/lib/mongoid/paranoia.rb +26 -26
  130. data/lib/mongoid/persistence.rb +15 -21
  131. data/lib/mongoid/persistence/atomic.rb +135 -0
  132. data/lib/mongoid/persistence/atomic/add_to_set.rb +11 -8
  133. data/lib/mongoid/persistence/atomic/bit.rb +37 -0
  134. data/lib/mongoid/persistence/atomic/inc.rb +9 -6
  135. data/lib/mongoid/persistence/atomic/operation.rb +48 -7
  136. data/lib/mongoid/persistence/atomic/pop.rb +34 -0
  137. data/lib/mongoid/persistence/atomic/pull.rb +34 -0
  138. data/lib/mongoid/persistence/atomic/pull_all.rb +10 -9
  139. data/lib/mongoid/persistence/atomic/push.rb +8 -5
  140. data/lib/mongoid/persistence/atomic/push_all.rb +31 -0
  141. data/lib/mongoid/persistence/atomic/rename.rb +31 -0
  142. data/lib/mongoid/persistence/atomic/set.rb +30 -0
  143. data/lib/mongoid/persistence/atomic/unset.rb +28 -0
  144. data/lib/mongoid/persistence/deletion.rb +32 -0
  145. data/lib/mongoid/persistence/insertion.rb +41 -0
  146. data/lib/mongoid/persistence/modification.rb +37 -0
  147. data/lib/mongoid/persistence/operations.rb +214 -0
  148. data/lib/mongoid/persistence/operations/embedded/insert.rb +42 -0
  149. data/lib/mongoid/persistence/operations/embedded/remove.rb +40 -0
  150. data/lib/mongoid/persistence/operations/insert.rb +34 -0
  151. data/lib/mongoid/persistence/operations/remove.rb +33 -0
  152. data/lib/mongoid/persistence/operations/update.rb +53 -0
  153. data/lib/mongoid/railtie.rb +21 -33
  154. data/lib/mongoid/railties/database.rake +12 -12
  155. data/lib/mongoid/relations.rb +9 -5
  156. data/lib/mongoid/relations/accessors.rb +15 -36
  157. data/lib/mongoid/relations/auto_save.rb +2 -2
  158. data/lib/mongoid/relations/binding.rb +28 -1
  159. data/lib/mongoid/relations/bindings/embedded/in.rb +17 -30
  160. data/lib/mongoid/relations/bindings/embedded/many.rb +16 -21
  161. data/lib/mongoid/relations/bindings/embedded/one.rb +11 -16
  162. data/lib/mongoid/relations/bindings/referenced/in.rb +31 -32
  163. data/lib/mongoid/relations/bindings/referenced/many.rb +19 -61
  164. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +15 -63
  165. data/lib/mongoid/relations/bindings/referenced/one.rb +18 -26
  166. data/lib/mongoid/relations/builder.rb +4 -2
  167. data/lib/mongoid/relations/builders.rb +21 -2
  168. data/lib/mongoid/relations/builders/embedded/in.rb +5 -1
  169. data/lib/mongoid/relations/builders/embedded/many.rb +12 -4
  170. data/lib/mongoid/relations/builders/embedded/one.rb +5 -1
  171. data/lib/mongoid/relations/builders/nested_attributes/many.rb +2 -2
  172. data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
  173. data/lib/mongoid/relations/builders/referenced/in.rb +2 -5
  174. data/lib/mongoid/relations/builders/referenced/many.rb +2 -3
  175. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +14 -5
  176. data/lib/mongoid/relations/builders/referenced/one.rb +2 -3
  177. data/lib/mongoid/relations/embedded/atomic.rb +2 -2
  178. data/lib/mongoid/relations/embedded/in.rb +72 -41
  179. data/lib/mongoid/relations/embedded/many.rb +116 -120
  180. data/lib/mongoid/relations/embedded/one.rb +59 -41
  181. data/lib/mongoid/relations/embedded/sort.rb +31 -0
  182. data/lib/mongoid/relations/macros.rb +28 -24
  183. data/lib/mongoid/relations/many.rb +10 -103
  184. data/lib/mongoid/relations/metadata.rb +335 -38
  185. data/lib/mongoid/relations/one.rb +7 -32
  186. data/lib/mongoid/relations/options.rb +47 -0
  187. data/lib/mongoid/relations/proxy.rb +29 -28
  188. data/lib/mongoid/relations/referenced/batch.rb +2 -3
  189. data/lib/mongoid/relations/referenced/in.rb +66 -53
  190. data/lib/mongoid/relations/referenced/many.rb +216 -143
  191. data/lib/mongoid/relations/referenced/many_to_many.rb +132 -163
  192. data/lib/mongoid/relations/referenced/one.rb +76 -58
  193. data/lib/mongoid/relations/synchronization.rb +113 -0
  194. data/lib/mongoid/relations/targets.rb +2 -0
  195. data/lib/mongoid/relations/targets/enumerable.rb +329 -0
  196. data/lib/mongoid/safety.rb +24 -156
  197. data/lib/mongoid/serialization.rb +21 -0
  198. data/lib/mongoid/state.rb +34 -0
  199. data/lib/mongoid/threaded.rb +175 -0
  200. data/lib/mongoid/timestamps/updated.rb +1 -1
  201. data/lib/mongoid/validations.rb +3 -7
  202. data/lib/mongoid/version.rb +1 -1
  203. data/lib/mongoid/versioning.rb +61 -7
  204. data/lib/rack/mongoid.rb +2 -0
  205. data/lib/rack/mongoid/middleware/identity_map.rb +38 -0
  206. data/lib/rails/generators/mongoid/model/model_generator.rb +1 -1
  207. data/lib/rails/generators/mongoid/model/templates/{model.rb → model.rb.tt} +0 -0
  208. data/lib/rails/generators/mongoid/observer/observer_generator.rb +1 -1
  209. data/lib/rails/generators/mongoid/observer/templates/{observer.rb → observer.rb.tt} +0 -0
  210. data/lib/rails/mongoid.rb +17 -17
  211. metadata +136 -102
  212. data/lib/mongoid/atomicity.rb +0 -111
  213. data/lib/mongoid/collections/cyclic_iterator.rb +0 -34
  214. data/lib/mongoid/collections/slaves.rb +0 -61
  215. data/lib/mongoid/extensions/array/conversions.rb +0 -23
  216. data/lib/mongoid/extensions/array/parentization.rb +0 -13
  217. data/lib/mongoid/extensions/big_decimal/conversions.rb +0 -19
  218. data/lib/mongoid/extensions/binary/conversions.rb +0 -17
  219. data/lib/mongoid/extensions/boolean/conversions.rb +0 -27
  220. data/lib/mongoid/extensions/date/conversions.rb +0 -25
  221. data/lib/mongoid/extensions/datetime/conversions.rb +0 -12
  222. data/lib/mongoid/extensions/float/conversions.rb +0 -20
  223. data/lib/mongoid/extensions/hash/conversions.rb +0 -19
  224. data/lib/mongoid/extensions/integer/conversions.rb +0 -20
  225. data/lib/mongoid/extensions/object/conversions.rb +0 -25
  226. data/lib/mongoid/extensions/range/conversions.rb +0 -25
  227. data/lib/mongoid/extensions/set/conversions.rb +0 -20
  228. data/lib/mongoid/extensions/symbol/conversions.rb +0 -21
  229. data/lib/mongoid/extensions/time_conversions.rb +0 -38
  230. data/lib/mongoid/field.rb +0 -162
  231. data/lib/mongoid/paths.rb +0 -61
  232. data/lib/mongoid/persistence/command.rb +0 -71
  233. data/lib/mongoid/persistence/insert.rb +0 -53
  234. data/lib/mongoid/persistence/insert_embedded.rb +0 -43
  235. data/lib/mongoid/persistence/remove.rb +0 -44
  236. data/lib/mongoid/persistence/remove_all.rb +0 -40
  237. data/lib/mongoid/persistence/remove_embedded.rb +0 -48
  238. data/lib/mongoid/persistence/update.rb +0 -77
  239. data/lib/mongoid/safe.rb +0 -23
  240. data/lib/mongoid/validations/referenced.rb +0 -58
@@ -1,16 +1,37 @@
1
- module Mongoid
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+
4
+ # The Mongoid logger which wraps some other ruby compliant logger class.
2
5
  class Logger
3
6
 
4
7
  delegate :info, :debug, :error, :fatal, :unknown, :to => :logger, :allow_nil => true
5
8
 
9
+ # Emit a warning log message.
10
+ #
11
+ # @example Log a warning.
12
+ # logger.warn("Danger")
13
+ #
14
+ # @param [ String ] message The warning message.
6
15
  def warn(message)
7
16
  logger.warn(message) if logger && logger.respond_to?(:warn)
8
17
  end
9
18
 
19
+ # Get the mongoid logger.
20
+ #
21
+ # @example Get the global logger.
22
+ # logger.logger
23
+ #
24
+ # @return [ Logger ] The logger.
10
25
  def logger
11
26
  Mongoid.logger
12
27
  end
13
28
 
29
+ # Inspect the logger.
30
+ #
31
+ # @example Inspect the logger.
32
+ # logger.inspect
33
+ #
34
+ # @return [ String ] The logger, inspected.
14
35
  def inspect
15
36
  "#<Mongoid::Logger:0x#{object_id.to_s(16)} @logger=#{logger.inspect}>"
16
37
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Checks that all values match.
4
6
  class All < Default
7
+
5
8
  # Return true if the attribute and first value in the hash are equal.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If the values match.
6
16
  def matches?(value)
7
17
  @attribute == value.values.first
8
18
  end
@@ -32,7 +32,7 @@ module Mongoid #:nodoc:
32
32
  #
33
33
  # @since 1.0.0
34
34
  def matches?(value)
35
- attribute.is_a?(Array) ? attribute.include?(value) : value === attribute
35
+ attribute.is_a?(Array) && !value.is_a?(Array) ? attribute.include?(value) : value === attribute
36
36
  end
37
37
 
38
38
  protected
@@ -1,10 +1,20 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Checks for existance.
4
6
  class Exists < Default
7
+
5
8
  # Return true if the attribute exists and checking for existence or
6
9
  # return true if the attribute does not exist and checking for
7
10
  # non-existence.
11
+ #
12
+ # @example Does anything exist?
13
+ # matcher.matches?({ :key => 10 })
14
+ #
15
+ # @param [ Hash ] value The values to check.
16
+ #
17
+ # @return [ true, false ] If a value exists.
8
18
  def matches?(value)
9
19
  @attribute.nil? != value.values.first
10
20
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Performs Greater Than matching.
4
6
  class Gt < Default
7
+
5
8
  # Return true if the attribute is greater than the value.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If a value exists.
6
16
  def matches?(value)
7
17
  determine(value, :>)
8
18
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Performs Greater than or equal to matching.
4
6
  class Gte < Default
7
+
5
8
  # Return true if the attribute is greater than or equal to the value.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If a value exists.
6
16
  def matches?(value)
7
17
  determine(value, :>=)
8
18
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Performs matching for any value in an array.
4
6
  class In < Default
7
+
5
8
  # Return true if the attribute is in the values.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If a value exists.
6
16
  def matches?(value)
7
17
  value.values.first.include?(@attribute)
8
18
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Performs less than matching.
4
6
  class Lt < Default
7
+
5
8
  # Return true if the attribute is less than the value.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If a value exists.
6
16
  def matches?(value)
7
17
  determine(value, :<)
8
18
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Performs less than or equal to matching.
4
6
  class Lte < Default
7
+
5
8
  # Return true if the attribute is less than or equal to the value.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If a value exists.
6
16
  def matches?(value)
7
17
  determine(value, :<=)
8
18
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Performs non-equivalency checks.
4
6
  class Ne < Default
7
+
5
8
  # Return true if the attribute and first value are not equal.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If a value exists.
6
16
  def matches?(value)
7
17
  @attribute != value.values.first
8
18
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Performs not in checking.
4
6
  class Nin < Default
7
+
5
8
  # Return true if the attribute is not in the value list.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If a value exists.
6
16
  def matches?(value)
7
17
  !value.values.first.include?(@attribute)
8
18
  end
@@ -17,11 +17,14 @@ module Mongoid #:nodoc:
17
17
  # @since 2.0.0.rc.7
18
18
  def matches?(conditions)
19
19
  conditions.each do |condition|
20
- key = condition.keys.first
21
- value = condition.values.first
22
- if Strategies.matcher(document, key, value).matches?(value)
23
- return true
20
+ res = true
21
+ condition.keys.each do |k|
22
+ key = k
23
+ value = condition[k]
24
+ res &&= Strategies.matcher(document, key, value).matches?(value)
25
+ break unless res
24
26
  end
27
+ return res if res
25
28
  end
26
29
  return false
27
30
  end
@@ -1,8 +1,18 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Matchers #:nodoc:
4
+
5
+ # Performs size checking.
4
6
  class Size < Default
7
+
5
8
  # Return true if the attribute size is equal to the first value.
9
+ #
10
+ # @example Do the values match?
11
+ # matcher.matches?({ :key => 10 })
12
+ #
13
+ # @param [ Hash ] value The values to check.
14
+ #
15
+ # @return [ true, false ] If a value exists.
6
16
  def matches?(value)
7
17
  @attribute.size == value.values.first
8
18
  end
@@ -1,11 +1,31 @@
1
- module Mongoid::MultiDatabase
2
- extend ActiveSupport::Concern
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
3
 
4
- module ClassMethods
4
+ # Adds multiple database support to documents.
5
+ module MultiDatabase
6
+ extend ActiveSupport::Concern
5
7
 
6
- def database; @database end
7
- def set_database(name)
8
- @database = name.to_s
8
+ module ClassMethods #:nodoc:
9
+
10
+ # Gets the database from the class.
11
+ #
12
+ # @example Get the database.
13
+ # Model.database
14
+ #
15
+ # @return [ Mongo::DB ] The database.
16
+ def database; @database end
17
+
18
+ # Set the database name.
19
+ #
20
+ # @example Set the database name.
21
+ # Model.set_database(:testing)
22
+ #
23
+ # @param [ Symbol ] name The database name.
24
+ #
25
+ # @return [ String ] The database name.
26
+ def set_database(name)
27
+ @database = name.to_s
28
+ end
9
29
  end
10
30
  end
11
31
  end
@@ -1,14 +1,19 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
 
4
+ # Adds Rails' multi-parameter attribute support to Mongoid.
5
+ #
4
6
  # @todo: Durran: This module needs an overhaul.
5
7
  module MultiParameterAttributes
6
- module Errors
7
- # Raised when an error occurred while doing a mass assignment to an attribute through the
8
- # <tt>attributes=</tt> method. The exception has an +attribute+ property that is the name of the
9
- # offending attribute.
8
+
9
+ module Errors #:nodoc:
10
+
11
+ # Raised when an error occurred while doing a mass assignment to an
12
+ # attribute through the <tt>attributes=</tt> method. The exception
13
+ # has an +attribute+ property that is the name of the offending attribute.
10
14
  class AttributeAssignmentError < Mongoid::Errors::MongoidError
11
15
  attr_reader :exception, :attribute
16
+
12
17
  def initialize(message, exception, attribute)
13
18
  @exception = exception
14
19
  @attribute = attribute
@@ -16,17 +21,32 @@ module Mongoid #:nodoc:
16
21
  end
17
22
  end
18
23
 
19
- # Raised when there are multiple errors while doing a mass assignment through the +attributes+
20
- # method. The exception has an +errors+ property that contains an array of AttributeAssignmentError
21
- # objects, each corresponding to the error while assigning to an attribute.
24
+ # Raised when there are multiple errors while doing a mass assignment
25
+ # through the +attributes+ method. The exception has an +errors+
26
+ # property that contains an array of AttributeAssignmentError
27
+ # objects, each corresponding to the error while assigning to an
28
+ # attribute.
22
29
  class MultiparameterAssignmentErrors < Mongoid::Errors::MongoidError
23
30
  attr_reader :errors
31
+
24
32
  def initialize(errors)
25
33
  @errors = errors
26
34
  end
27
35
  end
28
36
  end
29
37
 
38
+ # Process the provided attributes casting them to their proper values if a
39
+ # field exists for them on the document. This will be limited to only the
40
+ # attributes provided in the suppied +Hash+ so that no extra nil values get
41
+ # put into the document's attributes.
42
+ #
43
+ # @example Process the attributes.
44
+ # person.process(:title => "sir", :age => 40)
45
+ #
46
+ # @param [ Hash ] attrs The attributes to set.
47
+ # @param [ Boolean ] guard_protected_attributes False to skip mass assignment protection.
48
+ #
49
+ # @since 2.0.0.rc.7
30
50
  def process(attrs = nil, guard_protected_attributes = true)
31
51
  if attrs
32
52
  errors = []
@@ -45,15 +65,20 @@ module Mongoid #:nodoc:
45
65
  multi_parameter_attributes.each_pair do |key, values|
46
66
  begin
47
67
  values = (values.keys.min..values.keys.max).map { |i| values[i] }
48
- klass = self.class.fields[key].try(:type)
49
- attributes[key] = instantiate_object(klass, values)
68
+ field = self.class.fields[key]
69
+ attributes[key] = instantiate_object(field, values)
50
70
  rescue => e
51
- errors << Errors::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{key}", e, key)
71
+ errors << Errors::AttributeAssignmentError.new(
72
+ "error on assignment #{values.inspect} to #{key}", e, key
73
+ )
52
74
  end
53
75
  end
54
76
 
55
77
  unless errors.empty?
56
- raise Errors::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
78
+ raise(
79
+ Errors::MultiparameterAssignmentErrors.new(errors),
80
+ "#{errors.size} error(s) on assignment of multiparameter attributes"
81
+ )
57
82
  end
58
83
 
59
84
  super attributes, guard_protected_attributes
@@ -62,21 +87,19 @@ module Mongoid #:nodoc:
62
87
  end
63
88
  end
64
89
 
65
- protected
90
+ protected
66
91
 
67
- def instantiate_object(klass, values_with_empty_parameters)
92
+ def instantiate_object(field, values_with_empty_parameters)
68
93
  return nil if values_with_empty_parameters.all? { |v| v.nil? }
69
-
70
94
  values = values_with_empty_parameters.collect { |v| v.nil? ? 1 : v }
71
-
95
+ klass = field.type
72
96
  if klass == DateTime || klass == Date || klass == Time
73
- klass.send(:convert_to_time, values)
97
+ field.serialize(values)
74
98
  elsif klass
75
99
  klass.new *values
76
100
  else
77
101
  values
78
102
  end
79
103
  end
80
-
81
104
  end
82
105
  end
@@ -85,8 +85,7 @@ module Mongoid #:nodoc:
85
85
  #
86
86
  # @since 1.0.0
87
87
  def scope_stack
88
- scope_stack_for = Thread.current[:mongoid_scope_stack] ||= {}
89
- scope_stack_for[object_id] ||= []
88
+ Threaded.scope_stack[object_id] ||= []
90
89
  end
91
90
 
92
91
  # Get a criteria object for the class, ignoring default scoping.
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
+
4
+ # Mongoid's implementation of Rails' nested attributes.
3
5
  module NestedAttributes
4
6
  extend ActiveSupport::Concern
5
7
 
@@ -10,7 +12,8 @@ module Mongoid #:nodoc:
10
12
  delegate :nested_attributes, :to => "self.class"
11
13
  end
12
14
 
13
- module ClassMethods
15
+ module ClassMethods #:nodoc:
16
+
14
17
  REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |_, value| value.blank? } }
15
18
 
16
19
  # Used when needing to update related models from a parent relation. Can
@@ -1,7 +1,110 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
 
4
- # Mongoid observers hook into the lifecycle of documents.
4
+ # Observer classes respond to life cycle callbacks to implement trigger-like
5
+ # behavior outside the original class. This is a great way to reduce the
6
+ # clutter that normally comes when the model class is burdened with
7
+ # functionality that doesn't pertain to the core responsibility of the
8
+ # class. Mongoid's observers work similar to ActiveRecord's. Example:
9
+ #
10
+ # class CommentObserver < Mongoid::Observer
11
+ # def after_save(comment)
12
+ # Notifications.comment(
13
+ # "admin@do.com", "New comment was posted", comment
14
+ # ).deliver
15
+ # end
16
+ # end
17
+ #
18
+ # This Observer sends an email when a Comment#save is finished.
19
+ #
20
+ # class ContactObserver < Mongoid::Observer
21
+ # def after_create(contact)
22
+ # contact.logger.info('New contact added!')
23
+ # end
24
+ #
25
+ # def after_destroy(contact)
26
+ # contact.logger.warn("Contact with an id of #{contact.id} was destroyed!")
27
+ # end
28
+ # end
29
+ #
30
+ # This Observer uses logger to log when specific callbacks are triggered.
31
+ #
32
+ # == Observing a class that can't be inferred
33
+ #
34
+ # Observers will by default be mapped to the class with which they share a
35
+ # name. So CommentObserver will be tied to observing Comment,
36
+ # ProductManagerObserver to ProductManager, and so on. If you want to
37
+ # name your observer differently than the class you're interested in
38
+ # observing, you can use the Observer.observe class method which takes
39
+ # either the concrete class (Product) or a symbol for that class (:product):
40
+ #
41
+ # class AuditObserver < Mongoid::Observer
42
+ # observe :account
43
+ #
44
+ # def after_update(account)
45
+ # AuditTrail.new(account, "UPDATED")
46
+ # end
47
+ # end
48
+ #
49
+ # If the audit observer needs to watch more than one kind of object,
50
+ # this can be specified with multiple arguments:
51
+ #
52
+ # class AuditObserver < Mongoid::Observer
53
+ # observe :account, :balance
54
+ #
55
+ # def after_update(record)
56
+ # AuditTrail.new(record, "UPDATED")
57
+ # end
58
+ # end
59
+ #
60
+ # The AuditObserver will now act on both updates to Account and Balance
61
+ # by treating them both as records.
62
+ #
63
+ # == Available callback methods
64
+ #
65
+ # * after_initialize
66
+ # * before_validation
67
+ # * after_validation
68
+ # * before_create
69
+ # * around_create
70
+ # * after_create
71
+ # * before_update
72
+ # * around_update
73
+ # * after_update
74
+ # * before_save
75
+ # * around_save
76
+ # * after_save
77
+ # * before_destroy
78
+ # * around_destroy
79
+ # * after_destroy
80
+ #
81
+ # == Storing Observers in Rails
82
+ #
83
+ # If you're using Mongoid within Rails, observer classes are usually stored
84
+ # in +app/models+ with the naming convention of +app/models/audit_observer.rb+.
85
+ #
86
+ # == Configuration
87
+ #
88
+ # In order to activate an observer, list it in the +config.mongoid.observers+
89
+ # configuration setting in your +config/application.rb+ file.
90
+ #
91
+ # config.mongoid.observers = :comment_observer, :signup_observer
92
+ #
93
+ # Observers will not be invoked unless you define them in your
94
+ # application configuration.
95
+ #
96
+ # == Loading
97
+ #
98
+ # Observers register themselves with the model class that they observe,
99
+ # since it is the class that notifies them of events when they occur.
100
+ # As a side-effect, when an observer is loaded, its corresponding model
101
+ # class is loaded.
102
+ #
103
+ # Observers are loaded after the application initializers, so that
104
+ # observed models can make use of extensions. If by any chance you are
105
+ # using observed models in the initialization, you can
106
+ # still load their observers by calling +ModelObserver.instance+ before.
107
+ # Observers are singletons and that call instantiates and registers them.
5
108
  class Observer < ActiveModel::Observer
6
109
 
7
110
  # Instantiate the new observer. Will add all child observers as well.
@@ -9,7 +112,7 @@ module Mongoid #:nodoc:
9
112
  # @example Instantiate the observer.
10
113
  # Mongoid::Observer.new
11
114
  #
12
- # @since 2.0.0
115
+ # @since 2.0.0.rc.8
13
116
  def initialize
14
117
  super and observed_descendants.each { |klass| add_observer!(klass) }
15
118
  end
@@ -23,7 +126,7 @@ module Mongoid #:nodoc:
23
126
  #
24
127
  # @return [ Array<Class> ] The children.
25
128
  #
26
- # @since 2.0.0
129
+ # @since 2.0.0.rc.8
27
130
  def observed_descendants
28
131
  observed_classes.sum([]) { |klass| klass.descendants }
29
132
  end
@@ -35,7 +138,7 @@ module Mongoid #:nodoc:
35
138
  #
36
139
  # @param [ Class ] klass The child observer to add.
37
140
  #
38
- # @since 2.0.0
141
+ # @since 2.0.0.rc.8
39
142
  def add_observer!(klass)
40
143
  super and define_callbacks(klass)
41
144
  end
@@ -47,7 +150,7 @@ module Mongoid #:nodoc:
47
150
  #
48
151
  # @param [ Class ] klass The model to define them on.
49
152
  #
50
- # @since 2.0.0
153
+ # @since 2.0.0.rc.8
51
154
  def define_callbacks(klass)
52
155
  tap do |observer|
53
156
  observer_name = observer.class.name.underscore.gsub('/', '__')