mongoid 4.0.2 → 5.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -1
  3. data/README.md +14 -13
  4. data/lib/config/locales/en.yml +28 -28
  5. data/lib/mongoid.rb +28 -21
  6. data/lib/mongoid/atomic.rb +2 -4
  7. data/lib/mongoid/attributes.rb +7 -7
  8. data/lib/mongoid/attributes/processing.rb +4 -1
  9. data/lib/mongoid/attributes/readonly.rb +2 -2
  10. data/lib/mongoid/changeable.rb +4 -6
  11. data/lib/mongoid/clients.rb +142 -0
  12. data/lib/mongoid/clients/factory.rb +78 -0
  13. data/lib/mongoid/{sessions → clients}/options.rb +30 -19
  14. data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
  15. data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
  16. data/lib/mongoid/clients/validators.rb +2 -0
  17. data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
  18. data/lib/mongoid/composable.rb +3 -3
  19. data/lib/mongoid/config.rb +39 -41
  20. data/lib/mongoid/config/environment.rb +1 -1
  21. data/lib/mongoid/config/validators.rb +1 -1
  22. data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
  23. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  24. data/lib/mongoid/contextual/atomic.rb +11 -11
  25. data/lib/mongoid/contextual/command.rb +9 -6
  26. data/lib/mongoid/contextual/geo_near.rb +17 -1
  27. data/lib/mongoid/contextual/map_reduce.rb +12 -11
  28. data/lib/mongoid/contextual/memory.rb +2 -5
  29. data/lib/mongoid/contextual/mongo.rb +92 -82
  30. data/lib/mongoid/contextual/none.rb +13 -0
  31. data/lib/mongoid/copyable.rb +6 -1
  32. data/lib/mongoid/criteria.rb +36 -3
  33. data/lib/mongoid/document.rb +3 -4
  34. data/lib/mongoid/errors.rb +6 -6
  35. data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
  36. data/lib/mongoid/errors/no_client_config.rb +22 -0
  37. data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
  38. data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
  39. data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
  40. data/lib/mongoid/errors/no_default_client.rb +23 -0
  41. data/lib/mongoid/extensions/hash.rb +5 -1
  42. data/lib/mongoid/extensions/object.rb +3 -2
  43. data/lib/mongoid/extensions/set.rb +5 -5
  44. data/lib/mongoid/factory.rb +4 -2
  45. data/lib/mongoid/fields.rb +7 -2
  46. data/lib/mongoid/findable.rb +4 -1
  47. data/lib/mongoid/indexable.rb +15 -9
  48. data/lib/mongoid/persistable.rb +1 -2
  49. data/lib/mongoid/persistable/creatable.rb +2 -2
  50. data/lib/mongoid/persistable/deletable.rb +3 -3
  51. data/lib/mongoid/persistable/incrementable.rb +1 -1
  52. data/lib/mongoid/persistable/logical.rb +1 -1
  53. data/lib/mongoid/persistable/poppable.rb +1 -1
  54. data/lib/mongoid/persistable/pullable.rb +2 -2
  55. data/lib/mongoid/persistable/pushable.rb +2 -2
  56. data/lib/mongoid/persistable/renamable.rb +1 -1
  57. data/lib/mongoid/persistable/settable.rb +1 -1
  58. data/lib/mongoid/persistable/unsettable.rb +1 -1
  59. data/lib/mongoid/persistable/updatable.rb +2 -2
  60. data/lib/mongoid/persistable/upsertable.rb +1 -1
  61. data/lib/mongoid/query_cache.rb +98 -104
  62. data/lib/mongoid/railtie.rb +1 -21
  63. data/lib/mongoid/railties/database.rake +1 -1
  64. data/lib/mongoid/relations/builders.rb +3 -1
  65. data/lib/mongoid/relations/counter_cache.rb +1 -1
  66. data/lib/mongoid/relations/embedded/batchable.rb +3 -10
  67. data/lib/mongoid/relations/embedded/many.rb +4 -2
  68. data/lib/mongoid/relations/many.rb +1 -0
  69. data/lib/mongoid/relations/proxy.rb +6 -6
  70. data/lib/mongoid/relations/referenced/many.rb +2 -1
  71. data/lib/mongoid/relations/targets/enumerable.rb +11 -11
  72. data/lib/mongoid/relations/touchable.rb +1 -1
  73. data/lib/mongoid/reloadable.rb +2 -2
  74. data/lib/mongoid/scopable.rb +6 -17
  75. data/lib/mongoid/selectable.rb +1 -36
  76. data/lib/mongoid/serializable.rb +2 -2
  77. data/lib/mongoid/stateful.rb +0 -1
  78. data/lib/mongoid/tasks/database.rake +2 -2
  79. data/lib/mongoid/tasks/database.rb +23 -16
  80. data/lib/mongoid/threaded.rb +54 -33
  81. data/lib/mongoid/threaded/lifecycle.rb +21 -16
  82. data/lib/mongoid/traversable.rb +16 -1
  83. data/lib/mongoid/validatable.rb +1 -1
  84. data/lib/mongoid/validatable/queryable.rb +1 -1
  85. data/lib/mongoid/validatable/uniqueness.rb +3 -20
  86. data/lib/mongoid/version.rb +1 -1
  87. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
  88. data/lib/rails/mongoid.rb +2 -2
  89. data/spec/app/models/audio.rb +1 -1
  90. data/spec/app/models/band.rb +1 -0
  91. data/spec/app/models/company.rb +5 -0
  92. data/spec/app/models/label.rb +7 -0
  93. data/spec/app/models/pub.rb +6 -0
  94. data/spec/app/models/staff.rb +7 -0
  95. data/spec/app/models/store_as_dup_test1.rb +5 -0
  96. data/spec/app/models/store_as_dup_test2.rb +5 -0
  97. data/spec/config/mongoid.yml +7 -25
  98. data/spec/mongoid/atomic/paths_spec.rb +3 -11
  99. data/spec/mongoid/attributes/nested_spec.rb +16 -16
  100. data/spec/mongoid/attributes/readonly_spec.rb +80 -18
  101. data/spec/mongoid/attributes_spec.rb +3 -3
  102. data/spec/mongoid/changeable_spec.rb +70 -0
  103. data/spec/mongoid/clients/factory_spec.rb +284 -0
  104. data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
  105. data/spec/mongoid/clients_spec.rb +739 -0
  106. data/spec/mongoid/config/environment_spec.rb +14 -11
  107. data/spec/mongoid/config_spec.rb +33 -48
  108. data/spec/mongoid/contextual/atomic_spec.rb +1 -17
  109. data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
  110. data/spec/mongoid/contextual/mongo_spec.rb +26 -83
  111. data/spec/mongoid/contextual/none_spec.rb +15 -0
  112. data/spec/mongoid/copyable_spec.rb +35 -1
  113. data/spec/mongoid/criteria/findable_spec.rb +197 -0
  114. data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
  115. data/spec/mongoid/criteria_spec.rb +74 -91
  116. data/spec/mongoid/document_spec.rb +1 -1
  117. data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
  118. data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
  119. data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
  120. data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
  121. data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
  122. data/spec/mongoid/fields/localized_spec.rb +1 -0
  123. data/spec/mongoid/fields_spec.rb +1 -0
  124. data/spec/mongoid/findable_spec.rb +2 -23
  125. data/spec/mongoid/indexable_spec.rb +12 -8
  126. data/spec/mongoid/interceptable_spec.rb +15 -0
  127. data/spec/mongoid/persistable/settable_spec.rb +16 -0
  128. data/spec/mongoid/persistable/updatable_spec.rb +3 -2
  129. data/spec/mongoid/persistable_spec.rb +4 -4
  130. data/spec/mongoid/query_cache_spec.rb +13 -8
  131. data/spec/mongoid/relations/auto_save_spec.rb +1 -1
  132. data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
  133. data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
  134. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
  135. data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
  136. data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
  137. data/spec/mongoid/relations/proxy_spec.rb +28 -0
  138. data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
  139. data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
  140. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
  141. data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
  142. data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
  143. data/spec/mongoid/reloadable_spec.rb +6 -6
  144. data/spec/mongoid/scopable_spec.rb +41 -28
  145. data/spec/mongoid/selectable_spec.rb +6 -16
  146. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  147. data/spec/mongoid/tasks/database_spec.rb +2 -2
  148. data/spec/mongoid/threaded_spec.rb +0 -7
  149. data/spec/mongoid/traversable_spec.rb +2 -2
  150. data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
  151. data/spec/mongoid_spec.rb +13 -15
  152. data/spec/rails/mongoid_spec.rb +13 -4
  153. data/spec/spec_helper.rb +44 -27
  154. data/spec/support/authorization.rb +12 -0
  155. data/spec/support/expectations.rb +14 -0
  156. metadata +52 -59
  157. data/lib/mongoid/contextual/find_and_modify.rb +0 -69
  158. data/lib/mongoid/contextual/text_search.rb +0 -178
  159. data/lib/mongoid/criteria/#findable.rb# +0 -141
  160. data/lib/mongoid/errors/no_default_session.rb +0 -23
  161. data/lib/mongoid/errors/no_session_config.rb +0 -22
  162. data/lib/mongoid/log_subscriber.rb +0 -55
  163. data/lib/mongoid/positional.rb +0 -71
  164. data/lib/mongoid/sessions.rb +0 -125
  165. data/lib/mongoid/sessions/factory.rb +0 -131
  166. data/lib/mongoid/sessions/mongo_uri.rb +0 -93
  167. data/lib/mongoid/sessions/validators.rb +0 -2
  168. data/lib/mongoid/support/query_counter.rb +0 -23
  169. data/spec/helpers.rb +0 -18
  170. data/spec/mongoid/#atomic_spec.rb# +0 -365
  171. data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
  172. data/spec/mongoid/contextual/text_search_spec.rb +0 -209
  173. data/spec/mongoid/log_subscriber_spec.rb +0 -75
  174. data/spec/mongoid/positional_spec.rb +0 -222
  175. data/spec/mongoid/sessions/factory_spec.rb +0 -333
  176. data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
  177. data/spec/mongoid/sessions_spec.rb +0 -1252
@@ -51,6 +51,19 @@ module Mongoid
51
51
  # @since 4.0.0
52
52
  def exists?; false; end
53
53
 
54
+
55
+ # Allow pluck for null context.
56
+ #
57
+ # @example Allow pluck for null context.
58
+ # context.pluck(:name)
59
+ #
60
+ # @param [ String, Symbol, Array ] field or fields to pluck.
61
+ #
62
+ # @return [ Array ] Emtpy Array
63
+ def pluck(*args)
64
+ []
65
+ end
66
+
54
67
  # Create the new null context.
55
68
  #
56
69
  # @example Create the new context.
@@ -22,7 +22,12 @@ module Mongoid
22
22
  # _id and id field in the document would cause problems with Mongoid
23
23
  # elsewhere.
24
24
  attrs = clone_document.except("_id", "id")
25
- self.class.new(attrs)
25
+ begin
26
+ self.class.new(attrs)
27
+ rescue Errors::UnknownAttribute
28
+ self.class.send(:include, Attributes::Dynamic)
29
+ self.class.new(attrs)
30
+ end
26
31
  end
27
32
  alias :dup :clone
28
33
 
@@ -4,7 +4,6 @@ require "mongoid/criteria/inspectable"
4
4
  require "mongoid/criteria/marshalable"
5
5
  require "mongoid/criteria/modifiable"
6
6
  require "mongoid/criteria/scopable"
7
- require "mongoid/sessions/options"
8
7
 
9
8
  module Mongoid
10
9
 
@@ -23,7 +22,7 @@ module Mongoid
23
22
  include Marshalable
24
23
  include Modifiable
25
24
  include Scopable
26
- include Sessions::Options
25
+ include Clients::Options
27
26
 
28
27
  # Static array used to check with method missing - we only need to ever
29
28
  # instantiate once.
@@ -293,7 +292,7 @@ module Mongoid
293
292
  options.merge!(criteria.options)
294
293
  self.documents = criteria.documents.dup unless criteria.documents.empty?
295
294
  self.scoping_options = criteria.scoping_options
296
- self.inclusions = (inclusions + criteria.inclusions.dup).uniq
295
+ self.inclusions = (inclusions + criteria.inclusions).uniq
297
296
  self
298
297
  end
299
298
 
@@ -335,6 +334,9 @@ module Mongoid
335
334
  def only(*args)
336
335
  return clone if args.flatten.empty?
337
336
  args = args.flatten
337
+ if (args & Fields::IDS).empty?
338
+ args.unshift(:_id)
339
+ end
338
340
  if klass.hereditary?
339
341
  super(*args.push(:_type))
340
342
  else
@@ -342,6 +344,37 @@ module Mongoid
342
344
  end
343
345
  end
344
346
 
347
+ # Set the read preference for the criteria.
348
+ #
349
+ # @example Set the read preference.
350
+ # criteria.read(mode: :primary_preferred)
351
+ #
352
+ # @param [ Hash ] value The mode preference.
353
+ #
354
+ # @return [ Criteria ] The cloned criteria.
355
+ #
356
+ # @since 5.0.0
357
+ def read(value = nil)
358
+ clone.tap do |criteria|
359
+ criteria.options.merge!(read: value)
360
+ end
361
+ end
362
+
363
+ # Overriden to exclude _id from the fields.
364
+ #
365
+ # @example Exclude fields returned from the database.
366
+ # Band.without(:name)
367
+ #
368
+ # @param [ Array<Symbol> ] args The names of the fields.
369
+ #
370
+ # @return [ Criteria ] The cloned criteria.
371
+ #
372
+ # @since 4.0.3
373
+ def without(*args)
374
+ args -= Fields::IDS
375
+ super(*args)
376
+ end
377
+
345
378
  # Returns true if criteria responds to the given method.
346
379
  #
347
380
  # @example Does the criteria respond to the method?
@@ -1,5 +1,4 @@
1
1
  # encoding: utf-8
2
- require "mongoid/positional"
3
2
  require "mongoid/evolvable"
4
3
  require "mongoid/extensions"
5
4
  require "mongoid/errors"
@@ -168,7 +167,7 @@ module Mongoid
168
167
  embedded_relations.each_pair do |name, meta|
169
168
  without_autobuild do
170
169
  relation, stored = send(name), meta.store_as
171
- if attributes.has_key?(stored) || !relation.blank?
170
+ if attributes.key?(stored) || !relation.blank?
172
171
  if relation
173
172
  attributes[stored] = relation.as_document
174
173
  else
@@ -262,7 +261,7 @@ module Mongoid
262
261
  #
263
262
  # @since 2.4.0
264
263
  def model_key
265
- @model_cache_key ||= "#{self.class.model_name.cache_key}"
264
+ @model_cache_key ||= self.class.model_name.cache_key
266
265
  end
267
266
 
268
267
  # Implement this for calls to flatten on array.
@@ -327,7 +326,7 @@ module Mongoid
327
326
  #
328
327
  # @since 1.0.0
329
328
  def _types
330
- @_type ||= (descendants + [ self ]).uniq.map { |t| t.to_s }
329
+ @_type ||= (descendants + [ self ]).uniq.map(&:to_s)
331
330
  end
332
331
 
333
332
  # Set the i18n scope to overwrite ActiveModel.
@@ -22,17 +22,17 @@ require "mongoid/errors/invalid_time"
22
22
  require "mongoid/errors/invalid_value"
23
23
  require "mongoid/errors/inverse_not_found"
24
24
  require "mongoid/errors/mixed_relations"
25
- require "mongoid/errors/mixed_session_configuration"
25
+ require "mongoid/errors/mixed_client_configuration"
26
26
  require "mongoid/errors/nested_attributes_metadata_not_found"
27
- require "mongoid/errors/no_default_session"
27
+ require "mongoid/errors/no_default_client"
28
28
  require "mongoid/errors/no_environment"
29
29
  require "mongoid/errors/no_map_reduce_output"
30
30
  require "mongoid/errors/no_metadata"
31
31
  require "mongoid/errors/no_parent"
32
- require "mongoid/errors/no_session_config"
33
- require "mongoid/errors/no_sessions_config"
34
- require "mongoid/errors/no_session_database"
35
- require "mongoid/errors/no_session_hosts"
32
+ require "mongoid/errors/no_client_config"
33
+ require "mongoid/errors/no_clients_config"
34
+ require "mongoid/errors/no_client_database"
35
+ require "mongoid/errors/no_client_hosts"
36
36
  require "mongoid/errors/readonly_attribute"
37
37
  require "mongoid/errors/readonly_document"
38
38
  require "mongoid/errors/scope_overwrite"
@@ -2,23 +2,23 @@
2
2
  module Mongoid
3
3
  module Errors
4
4
 
5
- # This error is raised when a session configuration contains both a uri and
5
+ # This error is raised when a client configuration contains both a uri and
6
6
  # other standard options.
7
- class MixedSessionConfiguration < MongoidError
7
+ class MixedClientConfiguration < MongoidError
8
8
 
9
9
  # Initialize the error.
10
10
  #
11
11
  # @example Initialize the error.
12
- # MixedSessionConfiguration.new(:name, {})
12
+ # MixedClientConfiguration.new(:name, {})
13
13
  #
14
- # @param [ Symbol ] name The name of the session config.
14
+ # @param [ Symbol ] name The name of the client config.
15
15
  # @param [ Hash ] config The configuration options.
16
16
  #
17
17
  # @since 3.0.0
18
18
  def initialize(name, config)
19
19
  super(
20
20
  compose_message(
21
- "mixed_session_configuration",
21
+ "mixed_client_configuration",
22
22
  { name: name, config: config }
23
23
  )
24
24
  )
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Errors
4
+
5
+ # This error is raised when attempting to create a new client that does
6
+ # not have a named configuration.
7
+ class NoClientConfig < MongoidError
8
+
9
+ # Create the new error.
10
+ #
11
+ # @example Create the error.
12
+ # NoClientConfig.new(:secondary)
13
+ #
14
+ # @param [ String, Symbol ] name The name of the client.
15
+ #
16
+ # @since 3.0.0
17
+ def initialize(name)
18
+ super(compose_message("no_client_config", { name: name }))
19
+ end
20
+ end
21
+ end
22
+ end
@@ -2,13 +2,13 @@
2
2
  module Mongoid
3
3
  module Errors
4
4
 
5
- # This error is raised when a session is configured without a database.
6
- class NoSessionDatabase < MongoidError
5
+ # This error is raised when a client is configured without a database.
6
+ class NoClientDatabase < MongoidError
7
7
 
8
8
  # Create the new error.
9
9
  #
10
10
  # @example Create the new error.
11
- # NoSessionDatabase.new(:default, {}})
11
+ # NoClientDatabase.new(:default, {}})
12
12
  #
13
13
  # @param [ Symbol, String ] name The db config key.
14
14
  # @param [ Hash ] config The hash configuration options.
@@ -17,7 +17,7 @@ module Mongoid
17
17
  def initialize(name, config)
18
18
  super(
19
19
  compose_message(
20
- "no_session_database",
20
+ "no_client_database",
21
21
  { name: name, config: config }
22
22
  )
23
23
  )
@@ -2,13 +2,13 @@
2
2
  module Mongoid
3
3
  module Errors
4
4
 
5
- # This error is raised when a session is configured without hosts.
6
- class NoSessionHosts < MongoidError
5
+ # This error is raised when a client is configured without hosts.
6
+ class NoClientHosts < MongoidError
7
7
 
8
8
  # Create the new error.
9
9
  #
10
10
  # @example Create the new error.
11
- # NoSessionHosts.new(:default, {}})
11
+ # NoClientHosts.new(:default, {}})
12
12
  #
13
13
  # @param [ Symbol, String ] name The db config key.
14
14
  # @param [ Hash ] config The hash configuration options.
@@ -17,7 +17,7 @@ module Mongoid
17
17
  def initialize(name, config)
18
18
  super(
19
19
  compose_message(
20
- "no_session_hosts",
20
+ "no_client_hosts",
21
21
  { name: name, config: config }
22
22
  )
23
23
  )
@@ -2,18 +2,18 @@
2
2
  module Mongoid
3
3
  module Errors
4
4
 
5
- # This error is raised when no sessions exists in the database
5
+ # This error is raised when no clients exists in the database
6
6
  # configuration.
7
- class NoSessionsConfig < MongoidError
7
+ class NoClientsConfig < MongoidError
8
8
 
9
9
  # Create the new error.
10
10
  #
11
11
  # @example Create the error.
12
- # NoSessionsConfig.new
12
+ # NoClientsConfig.new
13
13
  #
14
14
  # @since 3.0.0
15
15
  def initialize
16
- super(compose_message("no_sessions_config", {}))
16
+ super(compose_message("no_clients_config", {}))
17
17
  end
18
18
  end
19
19
  end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Errors
4
+
5
+ # This error is raised when a default client is not defined.
6
+ class NoDefaultClient < MongoidError
7
+
8
+ # Create the new error with the defined client names.
9
+ #
10
+ # @example Create the new error.
11
+ # NoDefaultClient.new([ :secondary ])
12
+ #
13
+ # @param [ Array<Symbol> ] keys The defined clients.
14
+ #
15
+ # @since 3.0.0
16
+ def initialize(keys)
17
+ super(
18
+ compose_message("no_default_client", { keys: keys.join(", ") })
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -24,7 +24,11 @@ module Mongoid
24
24
  #
25
25
  # @since 3.0.0
26
26
  def __mongoize_object_id__
27
- update_values(&:__mongoize_object_id__)
27
+ if id = self['$oid']
28
+ BSON::ObjectId.from_string(id)
29
+ else
30
+ update_values(&:__mongoize_object_id__)
31
+ end
28
32
  end
29
33
 
30
34
  # Consolidate the key/values in the hash under an atomic $set.
@@ -115,8 +115,9 @@ module Mongoid
115
115
  #
116
116
  # @since 2.0.0.rc.1
117
117
  def ivar(name)
118
- if instance_variable_defined?("@_#{name}")
119
- return instance_variable_get("@_#{name}")
118
+ var_name = "@_#{name}"
119
+ if instance_variable_defined?(var_name)
120
+ return instance_variable_get(var_name)
120
121
  else
121
122
  false
122
123
  end
@@ -9,7 +9,7 @@ module Mongoid
9
9
  # @example Mongoize the object.
10
10
  # set.mongoize
11
11
  #
12
- # @return [ Hash ] The object mongoized.
12
+ # @return [ Array ] The object mongoized.
13
13
  #
14
14
  # @since 3.0.0
15
15
  def mongoize
@@ -21,9 +21,9 @@ module Mongoid
21
21
  # Convert the object from its mongo friendly ruby type to this type.
22
22
  #
23
23
  # @example Demongoize the object.
24
- # Set.demongoize({ "min" => 1, "max" => 5 })
24
+ # Set.demongoize([1, 2, 3])
25
25
  #
26
- # @param [ Hash ] object The object to demongoize.
26
+ # @param [ Array ] object The object to demongoize.
27
27
  #
28
28
  # @return [ Set ] The set.
29
29
  #
@@ -36,11 +36,11 @@ module Mongoid
36
36
  # type.
37
37
  #
38
38
  # @example Mongoize the object.
39
- # Set.mongoize(1..3)
39
+ # Set.mongoize(Set.new([1,2,3]))
40
40
  #
41
41
  # @param [ Set ] object The object to mongoize.
42
42
  #
43
- # @return [ Hash ] The object mongoized.
43
+ # @return [ Array ] The object mongoized.
44
44
  #
45
45
  # @since 3.0.0
46
46
  def mongoize(object)
@@ -5,6 +5,8 @@ module Mongoid
5
5
  module Factory
6
6
  extend self
7
7
 
8
+ TYPE = "_type".freeze
9
+
8
10
  # Builds a new +Document+ from the supplied attributes.
9
11
  #
10
12
  # @example Build the document.
@@ -16,7 +18,7 @@ module Mongoid
16
18
  #
17
19
  # @return [ Document ] The instantiated document.
18
20
  def build(klass, attributes = nil)
19
- type = (attributes || {})["_type"]
21
+ type = (attributes || {})[TYPE]
20
22
  if type && klass._types.include?(type)
21
23
  type.constantize.new(attributes)
22
24
  else
@@ -37,7 +39,7 @@ module Mongoid
37
39
  #
38
40
  # @return [ Document ] The instantiated document.
39
41
  def from_db(klass, attributes = nil, selected_fields = nil)
40
- type = (attributes || {})["_type"]
42
+ type = (attributes || {})[TYPE]
41
43
  if type.blank?
42
44
  klass.instantiate(attributes, selected_fields)
43
45
  else
@@ -32,6 +32,11 @@ module Mongoid
32
32
  time: Time
33
33
  }.with_indifferent_access
34
34
 
35
+ # Constant for all names of the id field in a document.
36
+ #
37
+ # @since 5.0.0
38
+ IDS = [ :_id, :id, '_id', 'id' ].freeze
39
+
35
40
  included do
36
41
  class_attribute :aliased_fields
37
42
  class_attribute :localized_fields
@@ -93,7 +98,7 @@ module Mongoid
93
98
  #
94
99
  # @since 2.4.0
95
100
  def apply_default(name)
96
- unless attributes.has_key?(name)
101
+ unless attributes.key?(name)
97
102
  if field = fields[name]
98
103
  default = field.eval_default(self)
99
104
  unless default.nil? || field.lazy?
@@ -358,7 +363,7 @@ module Mongoid
358
363
  field_options = field.options
359
364
 
360
365
  Fields.options.each_pair do |option_name, handler|
361
- if field_options.has_key?(option_name)
366
+ if field_options.key?(option_name)
362
367
  handler.call(self, field, field_options[option_name])
363
368
  end
364
369
  end
@@ -19,7 +19,9 @@ module Mongoid
19
19
  :each,
20
20
  :each_with_index,
21
21
  :extras,
22
- :find_and_modify,
22
+ :find_one_and_delete,
23
+ :find_one_and_replace,
24
+ :find_one_and_update,
23
25
  :find_or_create_by,
24
26
  :find_or_create_by!,
25
27
  :find_or_initialize_by,
@@ -34,6 +36,7 @@ module Mongoid
34
36
  :min,
35
37
  :none,
36
38
  :pluck,
39
+ :read,
37
40
  :sum,
38
41
  :text_search,
39
42
  :update,