mongoid 5.1.6 → 5.2.0.rc0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/config/locales/en.yml +15 -0
  5. data/lib/mongoid.rb +5 -0
  6. data/lib/mongoid/clients/factory.rb +2 -0
  7. data/lib/mongoid/config.rb +1 -0
  8. data/lib/mongoid/contextual/map_reduce.rb +20 -97
  9. data/lib/mongoid/contextual/memory.rb +1 -0
  10. data/lib/mongoid/contextual/mongo.rb +15 -13
  11. data/lib/mongoid/errors.rb +1 -0
  12. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +20 -0
  13. data/lib/mongoid/errors/mongoid_error.rb +1 -1
  14. data/lib/mongoid/extensions.rb +1 -0
  15. data/lib/mongoid/extensions/decimal128.rb +39 -0
  16. data/lib/mongoid/indexable/validators/options.rb +2 -1
  17. data/lib/mongoid/persistable/deletable.rb +3 -7
  18. data/lib/mongoid/query_cache.rb +2 -2
  19. data/lib/mongoid/version.rb +1 -1
  20. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +4 -0
  21. data/spec/app/models/band.rb +1 -0
  22. data/spec/config/mongoid.yml +5 -0
  23. data/spec/mongoid/clients/factory_spec.rb +8 -0
  24. data/spec/mongoid/clients_spec.rb +78 -1
  25. data/spec/mongoid/config_spec.rb +31 -0
  26. data/spec/mongoid/contextual/atomic_spec.rb +342 -76
  27. data/spec/mongoid/contextual/map_reduce_spec.rb +102 -135
  28. data/spec/mongoid/contextual/memory_spec.rb +316 -56
  29. data/spec/mongoid/contextual/mongo_spec.rb +367 -5
  30. data/spec/mongoid/criteria_spec.rb +19 -0
  31. data/spec/mongoid/extensions/decimal128_spec.rb +44 -0
  32. data/spec/mongoid/indexable_spec.rb +43 -0
  33. data/spec/mongoid/query_cache_spec.rb +34 -0
  34. data/spec/mongoid/relations/referenced/many_spec.rb +11 -0
  35. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +11 -0
  36. data/spec/mongoid/scopable_spec.rb +12 -0
  37. data/spec/spec_helper.rb +9 -0
  38. metadata +19 -9
  39. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30bdab646c31a7e88e39456de765a87455bbe1ff
4
- data.tar.gz: d8ebddf7e3b5dc3838bbc57277e34c5abce7dfc1
3
+ metadata.gz: fe14c770935c2292f42dcab9a0198ecf778e2a0d
4
+ data.tar.gz: e5af1a0ceb58047b47420f3374d86f29a1243a51
5
5
  SHA512:
6
- metadata.gz: 1812af097622c5a03c8819da5e55c0ebba39f1aace28a394a4e72b05a71532f9f710dda776d72c734d5326daa918d890ac8991ca7a34cd94296630fbfd32dda3
7
- data.tar.gz: 974bebd0d9e74d07c93c9b242328656fb572e21d1ed9de634c5e1268d736fe0e61dac80a6c99e16cfc7a1fbefd28a8d5553dc67360f051508bd83402b84a52df
6
+ metadata.gz: f3c567925e7f39d74f08294de83e4e33a67973655cecf39184675c5b72dc80fb565c736d1e43c43012c2be5adcdf709bffe9bec543826a2491ffbb7fd635cd0b
7
+ data.tar.gz: 8e7c1b6d23eed2225b768b77c1025ab70f78e2581c70d55af31b063a44961577c9d4a0ff103553f98ab8fe97b78e7482db85fca998434343768cd59ab67d02d1
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -76,6 +76,12 @@ en:
76
76
  resolution: "For access to the collection that the embedded document is
77
77
  in, use %{klass}#_root.collection, or do not attempt to persist an
78
78
  embedded document without a parent set."
79
+ in_memory_collation_not_supported:
80
+ message: "A collation option cannot be applied when querying documents in-memory."
81
+ summary: "The query being run against documents in memory has a collation option set.
82
+ A collation option is only supported if the query is executed on a MongoDB server
83
+ with version >= 3.4."
84
+ resolution: "Remove the collation option from the query."
79
85
  invalid_config_option:
80
86
  message: "Invalid configuration option: %{name}."
81
87
  summary: "A invalid configuration option was provided in your
@@ -134,9 +140,18 @@ en:
134
140
  \_\_min: 1\n
135
141
  \_\_max: 1\n
136
142
  \_\_bits: 26\n
143
+ \_\_key: 26\n
137
144
  \_\_bucket_size : 1\n
145
+ \_\_sphere_version : 1\n
146
+ \_\_text_version : 1\n
147
+ \_\_version : 1\n
138
148
  \_\_weights: { content: 1, title: 2 }\n
139
149
  \_\_expire_after_seconds: number_of_seconds\n
150
+ \_\_partial_filter_expression\n
151
+ \_\_storage_engine\n
152
+ \_\_language_override\n
153
+ \_\_default_language\n
154
+ \_\_collation\n
140
155
  Valid types are: 1, -1, '2d', '2dsphere', 'geoHaystack', 'text', 'hashed'\n\n
141
156
  Example:\n
142
157
  \_\_class Band\n
@@ -36,6 +36,11 @@ module Mongoid
36
36
  extend Loggable
37
37
  extend self
38
38
 
39
+ # A string added to the platform details of Ruby driver client handshake documents.
40
+ #
41
+ # @since 6.1.0
42
+ PLATFORM_DETAILS = "mongoid-#{VERSION}".freeze
43
+
39
44
  # The minimum MongoDB version supported.
40
45
  MONGODB_VERSION = "2.4.0"
41
46
 
@@ -71,6 +71,8 @@ module Mongoid
71
71
  def options(configuration)
72
72
  config = configuration.dup
73
73
  options = config.delete(:options) || {}
74
+ options[:platform] = PLATFORM_DETAILS
75
+ options[:app_name] = Mongoid::Config.app_name if Mongoid::Config.app_name
74
76
  options.reject{ |k, v| k == :hosts }.to_hash.symbolize_keys!
75
77
  end
76
78
  end
@@ -25,6 +25,7 @@ module Mongoid
25
25
  option :use_activesupport_time_zone, default: true
26
26
  option :use_utc, default: false
27
27
  option :log_level, default: :info
28
+ option :app_name, default: nil
28
29
 
29
30
  # Has Mongoid been configured? This is checking that at least a valid
30
31
  # client config exists.
@@ -32,12 +32,13 @@ module Mongoid
32
32
  #
33
33
  # @since 3.0.0
34
34
  def each
35
+ validate_out!
35
36
  if block_given?
36
- documents.each do |doc|
37
+ @map_reduce.each do |doc|
37
38
  yield doc
38
39
  end
39
40
  else
40
- to_enum
41
+ @map_reduce.to_enum
41
42
  end
42
43
  end
43
44
 
@@ -64,7 +65,7 @@ module Mongoid
64
65
  #
65
66
  # @since 3.0.0
66
67
  def finalize(function)
67
- command[:finalize] = function
68
+ @map_reduce = @map_reduce.finalize(function)
68
69
  self
69
70
  end
70
71
 
@@ -79,10 +80,9 @@ module Mongoid
79
80
  #
80
81
  # @since 3.0.0
81
82
  def initialize(collection, criteria, map, reduce)
82
- @collection, @criteria = collection, criteria
83
- command[:mapreduce] = collection.name.to_s
84
- command[:map], command[:reduce] = map, reduce
85
- apply_criteria_options
83
+ @collection = collection
84
+ @criteria = criteria
85
+ @map_reduce = @criteria.view.map_reduce(map, reduce)
86
86
  end
87
87
 
88
88
  # Get the number of documents that were input into the map/reduce.
@@ -106,7 +106,7 @@ module Mongoid
106
106
  #
107
107
  # @since 3.0.0
108
108
  def js_mode
109
- command[:jsMode] = true
109
+ @map_reduce = @map_reduce.js_mode(true)
110
110
  self
111
111
  end
112
112
 
@@ -134,7 +134,7 @@ module Mongoid
134
134
  normalized.update_values do |value|
135
135
  value.is_a?(::Symbol) ? value.to_s : value
136
136
  end
137
- command[:out] = normalized
137
+ @map_reduce = @map_reduce.out(normalized)
138
138
  self
139
139
  end
140
140
 
@@ -159,8 +159,12 @@ module Mongoid
159
159
  #
160
160
  # @since 3.0.0
161
161
  def raw
162
- results
162
+ validate_out!
163
+ cmd = command
164
+ opts = { read: cmd.delete(:read).options } if cmd[:read]
165
+ @map_reduce.database.command(cmd, opts || {}).first
163
166
  end
167
+ alias :results :raw
164
168
 
165
169
  # Execute the map/reduce, returning the raw output.
166
170
  # Useful when you don't care about map/reduce's ouptut.
@@ -196,7 +200,7 @@ module Mongoid
196
200
  #
197
201
  # @since 3.0.0
198
202
  def scope(object)
199
- command[:scope] = object
203
+ @map_reduce = @map_reduce.scope(object)
200
204
  self
201
205
  end
202
206
 
@@ -232,95 +236,14 @@ module Mongoid
232
236
  }
233
237
  end
234
238
 
235
- private
236
-
237
- # Apply criteria specific options - query, sort, limit.
238
- #
239
- # @api private
240
- #
241
- # @example Apply the criteria options
242
- # map_reduce.apply_criteria_options
243
- #
244
- # @return [ nil ] Nothing.
245
- #
246
- # @since 3.0.0
247
- def apply_criteria_options
248
- command[:query] = criteria.selector
249
- if sort = criteria.options[:sort]
250
- command[:sort] = sort
251
- end
252
- if limit = criteria.options[:limit]
253
- command[:limit] = limit
254
- end
239
+ def command
240
+ @map_reduce.send(:map_reduce_spec)[:selector]
255
241
  end
256
242
 
257
- # Get the result documents from the map/reduce. If the output was inline
258
- # then we grab them from the results key. If the output was a temp
259
- # collection then we need to execute a find on that collection.
260
- #
261
- # @api private
262
- #
263
- # @example Get the documents.
264
- # map_reduce.documents
265
- #
266
- # @return [ Array, Cursor ] The documents.
267
- #
268
- # @since 3.0.0
269
- def documents
270
- return results["results"] if results.has_key?("results")
271
- view = client[output_collection].find
272
- view.no_cursor_timeout if criteria.options[:timeout] == false
273
- view
274
- end
275
-
276
- # Get the collection that the map/reduce results were stored in.
277
- #
278
- # @api private
279
- #
280
- # @example Get the output collection.
281
- # map_reduce.output_collection
282
- #
283
- # @return [ Symbol, String ] The output collection.
284
- #
285
- # @since 3.0.0
286
- def output_collection
287
- command[:out].values.first
288
- end
289
-
290
- # Execute the map/reduce command and get the results.
291
- #
292
- # @api private
293
- #
294
- # @example Get the results.
295
- # map_reduce.results
296
- #
297
- # @return [ Hash ] The results of the command.
298
- #
299
- # @since 3.0.0
300
- def results
301
- raise Errors::NoMapReduceOutput.new(command) unless command[:out]
302
- @results ||= __client__.command(command,__client__.options).first
303
- end
243
+ private
304
244
 
305
- # Get the client with the proper consistency.
306
- #
307
- # @api private
308
- #
309
- # @note We can use eventual if the output is set to inline.
310
- #
311
- # @example Get the client.
312
- # map_reduce.__client__
313
- #
314
- # @return [ Mongo::Client ] The client with consistency set.
315
- #
316
- # @since 3.0.15
317
- def __client__
318
- if command[:out][:inline] != 1
319
- # @todo: close
320
- client.with(read: { mode: :primary })
321
- else
322
- client
323
- end
245
+ def validate_out!
246
+ raise Errors::NoMapReduceOutput.new({}) unless @map_reduce.out
324
247
  end
325
248
  end
326
249
  end
@@ -373,6 +373,7 @@ module Mongoid
373
373
  #
374
374
  # @since 3.0.0
375
375
  def apply_options
376
+ raise Errors::InMemoryCollationNotSupported.new if criteria.options[:collation]
376
377
  skip(criteria.options[:skip]).limit(criteria.options[:limit])
377
378
  end
378
379
 
@@ -18,7 +18,18 @@ module Mongoid
18
18
  # Options constant.
19
19
  #
20
20
  # @since 5.0.0
21
- OPTIONS = [ :hint, :limit, :skip, :sort, :batch_size, :max_scan, :snapshot, :comment, :read ].freeze
21
+ OPTIONS = [ :hint,
22
+ :limit,
23
+ :skip,
24
+ :sort,
25
+ :batch_size,
26
+ :max_scan,
27
+ :snapshot,
28
+ :comment,
29
+ :read,
30
+ :cursor_type,
31
+ :collation
32
+ ].freeze
22
33
 
23
34
  # @attribute [r] view The Mongo collection view.
24
35
  attr_reader :view
@@ -68,9 +79,7 @@ module Mongoid
68
79
  #
69
80
  # @since 3.0.0
70
81
  def delete
71
- self.count.tap do
72
- view.delete_many
73
- end
82
+ view.delete_many.deleted_count
74
83
  end
75
84
  alias :delete_all :delete
76
85
 
@@ -83,11 +92,10 @@ module Mongoid
83
92
  #
84
93
  # @since 3.0.0
85
94
  def destroy
86
- destroyed = self.count
87
- each do |doc|
95
+ each.inject(0) do |count, doc|
88
96
  doc.destroy
97
+ count += 1
89
98
  end
90
- destroyed
91
99
  end
92
100
  alias :destroy_all :destroy
93
101
 
@@ -551,12 +559,6 @@ module Mongoid
551
559
  if criteria.options[:timeout] == false
552
560
  @view = view.no_cursor_timeout
553
561
  end
554
- if criteria.options[:cursor_type]
555
- # @todo: update to use #cursor_type method on view when driver 2.3 is released.
556
- # See RUBY-1080
557
- @view = view.clone
558
- @view.options.merge!(cursor_type: criteria.options[:cursor_type])
559
- end
560
562
  end
561
563
 
562
564
  # Apply an option.
@@ -5,6 +5,7 @@ require "mongoid/errors/callback"
5
5
  require "mongoid/errors/document_not_destroyed"
6
6
  require "mongoid/errors/document_not_found"
7
7
  require "mongoid/errors/eager_load"
8
+ require "mongoid/errors/in_memory_collation_not_supported"
8
9
  require "mongoid/errors/invalid_collection"
9
10
  require "mongoid/errors/invalid_config_option"
10
11
  require "mongoid/errors/invalid_field"
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Errors
4
+
5
+ # This error is raised when attempting to do a query with a
6
+ # collation on documents in memory.
7
+ class InMemoryCollationNotSupported < MongoidError
8
+
9
+ # Create the new error.
10
+ #
11
+ # @example Create the new unsupported collation error.
12
+ # InMemoryCollationNotSupported.new
13
+ #
14
+ # @since 5.2.0
15
+ def initialize
16
+ super(compose_message("in_memory_collation_not_supported"))
17
+ end
18
+ end
19
+ end
20
+ end
@@ -19,7 +19,7 @@ module Mongoid
19
19
  # @return [ String ] The composed message.
20
20
  #
21
21
  # @since 3.0.0
22
- def compose_message(key, attributes)
22
+ def compose_message(key, attributes = {})
23
23
  @problem = translate_problem(key, attributes)
24
24
  @summary = translate_summary(key, attributes)
25
25
  @resolution = translate_resolution(key, attributes)
@@ -30,6 +30,7 @@ require "mongoid/extensions/big_decimal"
30
30
  require "mongoid/extensions/boolean"
31
31
  require "mongoid/extensions/date"
32
32
  require "mongoid/extensions/date_time"
33
+ require "mongoid/extensions/decimal128"
33
34
  require "mongoid/extensions/false_class"
34
35
  require "mongoid/extensions/float"
35
36
  require "mongoid/extensions/hash"
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Extensions
4
+ module Decimal128
5
+
6
+ # Evolve the decimal128.
7
+ #
8
+ # @example Evolve the decimal128.
9
+ # decimal128.__evolve_decimal128__
10
+ #
11
+ # @return [ BSON::Decimal128 ] self.
12
+ #
13
+ # @since 5.2.0
14
+ def __evolve_decimal128__
15
+ self
16
+ end
17
+
18
+ module ClassMethods
19
+
20
+ # Evolve the object into a mongo-friendly value to query with.
21
+ #
22
+ # @example Evolve the object.
23
+ # Decimal128.evolve(dec)
24
+ #
25
+ # @param [ Object ] object The object to evolve.
26
+ #
27
+ # @return [ BSON::Decimal128 ] The decimal128.
28
+ #
29
+ # @since 5.2.0
30
+ def evolve(object)
31
+ object.__evolve_decimal128__
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ BSON::Decimal128.__send__(:include, Mongoid::Extensions::Decimal128)
39
+ BSON::Decimal128.extend(Mongoid::Extensions::Decimal128::ClassMethods)
@@ -27,7 +27,8 @@ module Mongoid
27
27
  :sphere_version,
28
28
  :text_version,
29
29
  :version,
30
- :partial_filter_expression
30
+ :partial_filter_expression,
31
+ :collation
31
32
  ]
32
33
 
33
34
  VALID_TYPES = [
@@ -135,13 +135,9 @@ module Mongoid
135
135
  # @return [ Integer ] The number of documents deleted.
136
136
  #
137
137
  # @since 1.0.0
138
- def delete_all(conditions = nil)
139
- selector = conditions || {}
140
- selector.merge!(_type: name) if hereditary?
141
- coll = collection
142
- deleted = coll.find(selector).count
143
- coll.find(selector).delete_many
144
- deleted
138
+ def delete_all(conditions = {})
139
+ selector = hereditary? ? conditions.merge(_type: name) : conditions
140
+ collection.find(selector).delete_many.deleted_count
145
141
  end
146
142
  end
147
143
  end
@@ -236,7 +236,7 @@ module Mongoid
236
236
 
237
237
  def cached_cursor
238
238
  if limit
239
- key = [ collection.namespace, selector, nil, skip, sort, projection ]
239
+ key = [ collection.namespace, selector, nil, skip, sort, projection, collation ]
240
240
  cursor = QueryCache.cache_table[key]
241
241
  if cursor
242
242
  limited_docs = cursor.to_a[0...limit.abs]
@@ -247,7 +247,7 @@ module Mongoid
247
247
  end
248
248
 
249
249
  def cache_key
250
- [ collection.namespace, selector, limit, skip, sort, projection ]
250
+ [ collection.namespace, selector, limit, skip, sort, projection, collation ]
251
251
  end
252
252
 
253
253
  def system_collection?