forked from mongodb/mongoid
/
criteria.rb
374 lines (350 loc) · 11.5 KB
/
criteria.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# encoding: utf-8
require "mongoid/criterion/builder"
require "mongoid/criterion/creational"
require "mongoid/criterion/complex"
require "mongoid/criterion/exclusion"
require "mongoid/criterion/inclusion"
require "mongoid/criterion/inspection"
require "mongoid/criterion/optional"
require "mongoid/criterion/selector"
require "mongoid/criterion/unconvertable"
module Mongoid #:nodoc:
# The +Criteria+ class is the core object needed in Mongoid to retrieve
# objects from the database. It is a DSL that essentially sets up the
# selector and options arguments that get passed on to a Mongo::Collection
# in the Ruby driver. Each method on the +Criteria+ returns self to they
# can be chained in order to create a readable criterion to be executed
# against the database.
#
# @example Create and execute a criteria.
# criteria = Criteria.new
# criteria.only(:field).where(:field => "value").skip(20).limit(20)
# criteria.execute
class Criteria
include Enumerable
include Criterion::Builder
include Criterion::Creational
include Criterion::Exclusion
include Criterion::Inclusion
include Criterion::Inspection
include Criterion::Optional
attr_accessor \
:collection,
:documents,
:embedded,
:ids,
:klass,
:options,
:selector,
:field_list
delegate \
:add_to_set,
:aggregate,
:avg,
:blank?,
:count,
:delete,
:delete_all,
:destroy,
:destroy_all,
:distinct,
:empty?,
:execute,
:first,
:group,
:last,
:max,
:min,
:one,
:pull,
:shift,
:sum,
:update,
:update_all, :to => :context
# Concatinate the criteria with another enumerable. If the other is a
# +Criteria+ then it needs to get the collection from it.
#
# @example Concat 2 criteria.
# criteria + criteria
#
# @param [ Criteria ] other The other criteria.
def +(other)
entries + comparable(other)
end
# Returns the difference between the criteria and another enumerable. If
# the other is a +Criteria+ then it needs to get the collection from it.
#
# @example Get the difference of 2 criteria.
# criteria - criteria
#
# @param [ Criteria ] other The other criteria.
def -(other)
entries - comparable(other)
end
# Returns true if the supplied +Enumerable+ or +Criteria+ is equal to the results
# of this +Criteria+ or the criteria itself.
#
# @note This will force a database load when called if an enumerable is passed.
#
# @param [ Object ] other The other +Enumerable+ or +Criteria+ to compare to.
#
# @return [ true, false ] If the objects are equal.
def ==(other)
case other
when Criteria
self.selector == other.selector && self.options == other.options
when Enumerable
return (execute.entries == other)
else
return false
end
end
# Return or create the context in which this criteria should be executed.
#
# This will return an Enumerable context if the class is embedded,
# otherwise it will return a Mongo context for root classes.
#
# @example Get the appropriate context.
# criteria.context
#
# @return [ Mongo, Enumerable ] The appropriate context.
def context
@context ||= Contexts.context_for(self, embedded)
end
# Iterate over each +Document+ in the results. This can take an optional
# block to pass to each argument in the results.
#
# @example Iterate over the criteria results.
# criteria.each { |doc| p doc }
#
# @return [ Criteria ] The criteria itself.
def each(&block)
tap { context.iterate(&block) }
end
# Return true if the criteria has some Document or not.
#
# @example Are there any documents for the criteria?
# criteria.exists?
#
# @return [ true, false ] If documents match.
def exists?
context.count > 0
end
# When freezing a criteria we need to initialize the context first
# otherwise the setting of the context on attempted iteration will raise a
# runtime error.
#
# @example Freeze the criteria.
# criteria.freeze
#
# @return [ Criteria ] The frozen criteria.
#
# @since 2.0.0
def freeze
context and super
end
# Merges the supplied argument hash into a single criteria
#
# @example Fuse the criteria and the object.
# criteria.fuse(:where => { :field => "value"}, :limit => 20)
#
# @param [ Hash ] criteria_conditions Criteria keys and values.
#
# @return [ Criteria ] self.
def fuse(criteria_conditions = {})
criteria_conditions.inject(self) do |criteria, (key, value)|
criteria.send(key, value)
end
end
# Create the new +Criteria+ object. This will initialize the selector
# and options hashes, as well as the type of criteria.
#
# @example Instantiate a new criteria.
# Criteria.new(Model, true)
#
# @param [ Class ] klass The model the criteria is for.
# @param [ true, false ] embedded Is the criteria for embedded docs.
def initialize(klass, embedded = false)
@selector = Criterion::Selector.new(klass)
@options, @klass, @documents, @embedded = {}, klass, [], embedded
end
# Merges another object into this +Criteria+. The other object may be a
# +Criteria+ or a +Hash+. This is used to combine multiple scopes together,
# where a chained scope situation may be desired.
#
# @example Merge the criteria with a conditions hash.
# criteria.merge({ :conditions => { :title => "Sir" } })
#
# @example Merge the criteria with another criteria.
# criteri.merge(other_criteria)
#
# @param [ Criteria, Hash ] other The other criterion to merge with.
#
# @return [ Criteria ] A cloned self.
def merge(other)
clone.tap do |crit|
if other.is_a?(Criteria)
crit.selector.update(other.selector)
crit.options.update(other.options)
crit.documents = other.documents
else
duped = other.dup
crit.selector.update(duped.delete(:conditions) || {})
crit.options.update(duped)
end
end
end
# Returns true if criteria responds to the given method.
#
# @example Does the criteria respond to the method?
# crtiteria.respond_to?(:each)
#
# @param [ Symbol ] name The name of the class method on the +Document+.
# @param [ true, false ] include_private Whether to include privates.
#
# @return [ true, false ] If the criteria responds to the method.
def respond_to?(name, include_private = false)
# don't include klass private methods because method_missing won't call them
super || @klass.respond_to?(name) || entries.respond_to?(name, include_private)
end
# Returns the selector and options as a +Hash+ that would be passed to a
# scope for use with named scopes.
#
# @example Get the criteria as a scoped hash.
# criteria.scoped
#
# @return [ Hash ] The criteria as a scoped hash.
def scoped
scope_options = @options.dup
sorting = scope_options.delete(:sort)
scope_options[:order_by] = sorting if sorting
{ :where => @selector }.merge(scope_options)
end
alias :to_ary :to_a
# Needed to properly get a criteria back as json
#
# @example Get the criteria as json.
# Person.where(:title => "Sir").as_json
#
# @param [ Hash ] options Options to pass through to the serializer.
#
# @return [ String ] The JSON string.
def as_json(options = nil)
entries.as_json(options)
end
# Search for documents based on a variety of args.
#
# @example Find by an id.
# criteria.search(BSON::ObjectId.new)
#
# @example Find by multiple ids.
# criteria.search([ BSON::ObjectId.new, BSON::ObjectId.new ])
#
# @example Conditionally find all matching documents.
# criteria.search(:all, :conditions => { :title => "Sir" })
#
# @example Conditionally find the first document.
# criteria.search(:first, :conditions => { :title => "Sir" })
#
# @example Conditionally find the last document.
# criteria.search(:last, :conditions => { :title => "Sir" })
#
# @param [ Symbol, BSON::ObjectId, Array<BSON::ObjectId> ] arg The
# argument to search with.
# @param [ Hash ] options The options to search with.
#
# @return [ Array<Symbol, Criteria> ] The type and criteria.
#
# @since 2.0.0
def search(*args)
raise_invalid if args[0].nil?
type = args[0]
params = args[1] || {}
return [ :ids, for_ids(type) ] unless type.is_a?(Symbol)
conditions = params.delete(:conditions) || {}
if conditions.include?(:id)
conditions[:_id] = conditions[:id]
conditions.delete(:id)
end
return [ type, where(conditions).extras(params) ]
end
# Convenience method of raising an invalid options error.
#
# @example Raise the error.
# criteria.raise_invalid
#
# @raise [ Errors::InvalidOptions ] The error.
#
# @since 2.0.0
def raise_invalid
raise Errors::InvalidOptions.new(:calling_document_find_with_nil_is_invalid, {})
end
protected
# Return the entries of the other criteria or the object. Used for
# comparing criteria or an enumerable.
#
# @example Get the comparable version.
# criteria.comparable(other)
#
# @param [ Criteria ] other Another criteria.
#
# @return [ Array ] The array to compare with.
def comparable(other)
other.is_a?(Criteria) ? other.entries : other
end
# Clone or dup the current +Criteria+. This will return a new criteria with
# the selector, options, klass, embedded options, etc intact.
#
# @example Clone a criteria.
# criteria.clone
#
# @example Dup a criteria.
# criteria.dup
#
# @param [ Criteria ] other The criteria getting cloned.
#
# @return [ nil ] nil.
def initialize_copy(other)
@selector = other.selector.dup
@options = other.options.dup
@context = nil
end
# Used for chaining +Criteria+ scopes together in the for of class methods
# on the +Document+ the criteria is for.
def method_missing(name, *args, &block)
if @klass.respond_to?(name)
@klass.send(:with_scope, self) do
@klass.send(name, *args, &block)
end
else
return entries.send(name, *args)
end
end
# Update the selector setting the operator on the value for each key in the
# supplied attributes +Hash+.
#
# @example Update the selector.
# criteria.update_selector({ :field => "value" }, "$in")
#
# @param [ Hash, Array ] attributes The values to convert and apply.
# @param [ String ] operator The MongoDB operator.
# @param [ Symbol ] combine The operator to use when combining sets.
def update_selector(attributes, operator, combine = :+)
clone.tap do |crit|
converted = BSON::ObjectId.convert(klass, attributes || {})
converted.each_pair do |key, value|
unless crit.selector[key]
crit.selector[key] = { operator => value }
else
if crit.selector[key].has_key?(operator)
new_value = crit.selector[key].values.first.send(combine, value)
crit.selector[key] = { operator => new_value }
else
crit.selector[key][operator] = value
end
end
end
end
end
end
end