mongoid 2.0.2 → 2.1.0

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 (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,25 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Extensions #:nodoc:
4
- module Range #:nodoc:
5
- module Conversions #:nodoc:
6
- extend ActiveSupport::Concern
7
-
8
- def to_hash
9
- { "min" => min, "max" => max }
10
- end
11
-
12
- module ClassMethods #:nodoc:
13
-
14
- def get(value)
15
- value.nil? ? nil : ::Range.new(value["min"], value["max"])
16
- end
17
-
18
- def set(value)
19
- value.nil? ? nil : value.to_hash
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,20 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Extensions #:nodoc:
4
- module Set #:nodoc:
5
- # This module converts set into mongoid related objects.
6
- module Conversions #:nodoc:
7
- extend ActiveSupport::Concern
8
-
9
- module ClassMethods #:nodoc:
10
- def get(value)
11
- ::Set.new(value)
12
- end
13
- def set(value)
14
- value.to_a
15
- end
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,21 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Extensions #:nodoc:
4
- module Symbol#:nodoc:
5
- # This module converts objects into mongoid related objects.
6
- module Conversions #:nodoc:
7
- extend ActiveSupport::Concern
8
-
9
- module ClassMethods
10
- def set(value)
11
- (value.nil? or (value.respond_to?(:empty?) && value.empty?)) ? nil : value.to_sym
12
- end
13
-
14
- def get(value)
15
- value
16
- end
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,38 +0,0 @@
1
- module Mongoid #:nodoc:
2
- module Extensions #:nodoc:
3
- module TimeConversions #:nodoc:
4
- def set(value)
5
- return nil if value.blank?
6
- time = convert_to_time(value)
7
- strip_milliseconds(time).utc
8
- end
9
-
10
- def get(value)
11
- return nil if value.blank?
12
- value = value.getlocal unless Mongoid::Config.use_utc?
13
- if Mongoid::Config.use_activesupport_time_zone?
14
- time_zone = Mongoid::Config.use_utc? ? 'UTC' : Time.zone
15
- value = value.in_time_zone(time_zone)
16
- end
17
- value
18
- end
19
-
20
- protected
21
-
22
- def strip_milliseconds(time)
23
- ::Time.at(time.to_i)
24
- end
25
-
26
- def convert_to_time(value)
27
- time = Mongoid::Config.use_activesupport_time_zone? ? ::Time.zone : ::Time
28
- case value
29
- when ::String then time.parse(value)
30
- when ::DateTime then time.local(value.year, value.month, value.day, value.hour, value.min, value.sec)
31
- when ::Date then time.local(value.year, value.month, value.day)
32
- when ::Array then time.local(*value)
33
- else value
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,162 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
-
4
- # Defines the behaviour for defined fields in the document.
5
- class Field
6
-
7
- NO_CAST_ON_READ = [
8
- Array, Binary, Boolean, Float, Hash,
9
- Integer, BSON::ObjectId, Set, String, Symbol
10
- ]
11
-
12
- attr_accessor :type
13
- attr_reader :copyable, :klass, :label, :name, :options
14
-
15
- class << self
16
-
17
- # Return a map of custom option names to their handlers.
18
- #
19
- # @example
20
- # Mongoid::Field.options
21
- # # => { :required => #<Proc:0x00000100976b38> }
22
- #
23
- # @return [ Hash ] the option map
24
- def options
25
- @options ||= {}
26
- end
27
-
28
- # Stores the provided block to be run when the option name specified is
29
- # defined on a field.
30
- #
31
- # No assumptions are made about what sort of work the handler might
32
- # perform, so it will always be called if the `option_name` key is
33
- # provided in the field definition -- even if it is false or nil.
34
- #
35
- # @example
36
- # Mongoid::Field.option :required do |model, field, value|
37
- # model.validates_presence_of field if value
38
- # end
39
- #
40
- # @param [ Symbol ] option_name the option name to match against
41
- # @param [ Proc ] block the handler to execute when the option is
42
- # provided.
43
- def option(option_name, &block)
44
- options[option_name] = block
45
- end
46
-
47
- end
48
-
49
- # When reading the field do we need to cast the value? This holds true when
50
- # times are stored or for big decimals which are stored as strings.
51
- #
52
- # @example Typecast on a read?
53
- # field.cast_on_read?
54
- #
55
- # @return [ true, false ] If the field should be cast.
56
- def cast_on_read?
57
- !NO_CAST_ON_READ.include?(type)
58
- end
59
-
60
- # Get the default value for the field.
61
- #
62
- # @example Get the default.
63
- # field.default
64
- #
65
- # @return [ Object ] The typecast default value.
66
- #
67
- # @since 1.0.0
68
- def default
69
- copy.respond_to?(:call) ? copy : set(copy)
70
- end
71
-
72
- # Create the new field with a name and optional additional options.
73
- #
74
- # @example Create the new field.
75
- # Field.new(:name, :type => String)
76
- #
77
- # @param [ Hash ] options The field options.
78
- #
79
- # @option options [ Class ] :type The class of the field.
80
- # @option options [ Object ] :default The default value for the field.
81
- # @option options [ String ] :label The field's label.
82
- #
83
- # @since 1.0.0
84
- def initialize(name, options = {})
85
- @type = options[:type] || Object
86
- @name, @default, @label = name, options[:default], options[:label]
87
- @copyable = (@default.is_a?(Array) || @default.is_a?(Hash))
88
- @options = options
89
- check_default!
90
- end
91
-
92
- # Used for setting an object in the attributes hash.
93
- #
94
- # If nil is provided the default will get returned if it exists.
95
- #
96
- # If the field is an identity field, ie an id, it performs the necessary
97
- # cast.
98
- #
99
- # @example Get the setter value.
100
- # field.set("New Value")
101
- #
102
- # @param [ Object ] object The value to cast to a database value.
103
- #
104
- # @return [ Object ] The typecast value.
105
- #
106
- # @since 1.0.0
107
- def set(object)
108
- unless options[:identity]
109
- type.set(object)
110
- else
111
- if object.blank?
112
- type.set(object) if object.is_a?(Array)
113
- else
114
- options[:metadata].constraint.convert(object)
115
- end
116
- end
117
- end
118
-
119
- # Used for retrieving the object out of the attributes hash.
120
- #
121
- # @example Get the value.
122
- # field.get("Value")
123
- #
124
- # @param [ Object ] The object to cast from the database.
125
- #
126
- # @return [ Object ] The converted value.
127
- #
128
- # @since 1.0.0
129
- def get(object)
130
- type.get(object)
131
- end
132
-
133
- protected
134
-
135
- # Copy the default value if copyable.
136
- #
137
- # @example Copy the default.
138
- # field.copy
139
- #
140
- # @return [ Object ] The copied object or the original.
141
- #
142
- # @since 1.0.0
143
- def copy
144
- copyable ? @default.dup : @default
145
- end
146
-
147
- # Checks if the default value is of the same type as the field.
148
- #
149
- # @example Check the default value.
150
- # field.check_default!
151
- #
152
- # @raise [ Errors::InvalidType ] If the types differ.
153
- #
154
- # @since 1.0.0
155
- def check_default!
156
- return if @default.is_a?(Proc)
157
- if !@default.nil? && !@default.is_a?(type)
158
- raise Mongoid::Errors::InvalidType.new(type, @default)
159
- end
160
- end
161
- end
162
- end
@@ -1,61 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Paths #:nodoc:
4
- extend ActiveSupport::Concern
5
- included do
6
- cattr_accessor :__path
7
- attr_accessor :_index
8
- end
9
-
10
- # Get the insertion modifier for the document. Will be nil on root
11
- # documents, $set on embeds_one, $push on embeds_many.
12
- #
13
- # Example:
14
- #
15
- # <tt>name.inserter</tt>
16
- def _inserter
17
- embedded? ? (embedded_many? ? "$push" : "$set") : nil
18
- end
19
-
20
- # Return the path to this +Document+ in JSON notation, used for atomic
21
- # updates via $set in MongoDB.
22
- #
23
- # Example:
24
- #
25
- # <tt>address.path # returns "addresses"</tt>
26
- def _path
27
- _position.sub!(/\.\d+$/, '') || _position
28
- end
29
- alias :_pull :_path
30
-
31
- # Returns the positional operator of this document for modification.
32
- #
33
- # Example:
34
- #
35
- # <tt>address.position</tt>
36
- def _position
37
- locator = _index ? (new_record? ? "" : ".#{_index}") : ""
38
- embedded? ? "#{_parent._position}#{"." unless _parent._position.blank?}#{metadata.name.to_s}#{locator}" : ""
39
- end
40
-
41
- # Get the removal modifier for the document. Will be nil on root
42
- # documents, $unset on embeds_one, $set on embeds_many.
43
- #
44
- # Example:
45
- #
46
- # <tt>name.remover</tt>
47
- def _remover
48
- embedded? ? (_index ? "$pull" : "$unset") : nil
49
- end
50
-
51
- # Return the selector for this document to be matched exactly for use
52
- # with MongoDB's $ operator.
53
- #
54
- # Example:
55
- #
56
- # <tt>address.selector</tt>
57
- def _selector
58
- (embedded? ? _parent._selector.merge("#{_path}._id" => id) : { "_id" => id }).merge(shard_key_selector)
59
- end
60
- end
61
- end
@@ -1,71 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Persistence #:nodoc:
4
- # Persistence commands extend from this class to get basic functionality on
5
- # initialization.
6
- class Command
7
- include Mongoid::Safe
8
-
9
- attr_reader \
10
- :collection,
11
- :document,
12
- :klass,
13
- :options,
14
- :selector,
15
- :suppress,
16
- :validate
17
-
18
- # Initialize the persistence +Command+.
19
- #
20
- # Options:
21
- #
22
- # document_or_class: The +Document+ or +Class+ to get the collection.
23
- # options: Options like validation or safe mode.
24
- # selector: Optional selector to use in query.
25
- #
26
- # Example:
27
- #
28
- # <tt>DeleteAll.new(Person, { :validate => true }, {})</tt>
29
- def initialize(document_or_class, options = {}, selector = {})
30
- init(document_or_class)
31
- validate = options[:validate]
32
- @suppress = options[:suppress]
33
- @validate = (validate.nil? ? true : validate)
34
- @selector = selector
35
- @options = { :safe => safe_mode?(options) }
36
- end
37
-
38
- private
39
-
40
- # Setup the proper instance variables based on if the supplied argument
41
- # was a document object or a class object.
42
- #
43
- # Example:
44
- #
45
- # <tt>init(document_or_class)</tt>
46
- #
47
- # Options:
48
- #
49
- # document_or_class: A document or a class.
50
- def init(document_or_class)
51
- if document_or_class.is_a?(Mongoid::Document)
52
- @document = document_or_class
53
- @collection = @document.embedded? ? @document._root.collection : @document.collection
54
- else
55
- @klass = document_or_class
56
- @collection = @klass.collection
57
- end
58
- end
59
-
60
- # Should we suppress parent notifications?
61
- #
62
- # @example Suppress notifications?
63
- # command.suppress?
64
- #
65
- # @return [ true, false ] Should the parent notifcations be suppressed.
66
- def suppress?
67
- !!@suppress
68
- end
69
- end
70
- end
71
- end
@@ -1,53 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Persistence #:nodoc:
4
-
5
- # Insert is a persistence command responsible for taking a document that
6
- # has not been saved to the database and saving it.
7
- #
8
- # The underlying query resembles the following MongoDB query:
9
- #
10
- # collection.insert(
11
- # { "_id" : 1, "field" : "value" },
12
- # false
13
- # );
14
- class Insert < Command
15
-
16
- # Insert the new document in the database. This delegates to the standard
17
- # MongoDB collection's insert command.
18
- #
19
- # Example:
20
- #
21
- # <tt>Insert.persist</tt>
22
- #
23
- # Returns:
24
- #
25
- # The +Document+, whether the insert succeeded or not.
26
- def persist
27
- return document if validate && document.invalid?(:create)
28
- document.run_callbacks(:save) do
29
- document.run_callbacks(:create) do
30
- if insert
31
- document.new_record = false
32
- document._children.each { |child| child.new_record = false }
33
- document.move_changes
34
- end
35
- end
36
- end; document
37
- end
38
-
39
- protected
40
- # Insert the document into the database.
41
- def insert
42
- if document.embedded?
43
- Persistence::InsertEmbedded.new(
44
- document,
45
- options.merge(:validate => validate)
46
- ).persist
47
- else
48
- collection.insert(document.as_document, options)
49
- end
50
- end
51
- end
52
- end
53
- end