/
findable.rb
183 lines (169 loc) · 5.31 KB
/
findable.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
# frozen_string_literal: true
# rubocop:todo all
module Mongoid
class Criteria
# Mixin module included in Mongoid::Criteria which adds the ability
# to find document by id.
module Findable
# Execute the criteria or raise an error if no documents found.
#
# @example Execute or raise
# criteria.execute_or_raise(id)
#
# @param [ Object ] ids The arguments passed.
# @param [ true | false ] multi Whether there arguments were a list,
# and therefore the return value should be an array.
#
# @raise [ Errors::DocumentNotFound ] If nothing returned.
#
# @return [ Document | Array<Document> ] The document(s).
def execute_or_raise(ids, multi)
result = multiple_from_db(ids)
check_for_missing_documents!(result, ids)
multi ? result : result.first
end
# Find the matching document(s) in the criteria for the provided id(s).
#
# @note Each argument can be an individual id, an array of ids or
# a nested array. Each array will be flattened.
#
# @example Find by an id.
# criteria.find(BSON::ObjectId.new)
#
# @example Find by multiple ids.
# criteria.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
#
# @param [ [ Object | Array<Object> ]... ] *args The id(s) to find.
#
# @return [ Document | Array<Document> ] The matching document(s).
def find(*args)
ids = prepare_ids_for_find(args)
raise_invalid if ids.any?(&:nil?)
for_ids(ids).execute_or_raise(ids, multi_args?(args))
end
# Adds a criterion to the +Criteria+ that specifies an id that must be matched.
#
# @example Add a single id criteria.
# criteria.for_ids([ 1 ])
#
# @example Add multiple id criteria.
# criteria.for_ids([ 1, 2 ])
#
# @param [ Array ] ids The array of ids.
#
# @return [ Criteria ] The cloned criteria.
def for_ids(ids)
ids = mongoize_ids(ids)
if ids.size > 1
send(id_finder, { _id: { "$in" => ids }})
else
send(id_finder, { _id: ids.first })
end
end
# Get the documents from the identity map, and if not found hit the
# database.
#
# @example Get the documents from the map or criteria.
# criteria.multiple_from_map_or_db(ids)
#
# @param [ Array<Object> ] ids The searched ids.
#
# @return [ Array<Document> ] The found documents.
def multiple_from_db(ids)
return entries if embedded?
ids = mongoize_ids(ids)
ids.empty? ? [] : from_database(ids)
end
private
# Get the finder used to generate the id query.
#
# @api private
#
# @example Get the id finder.
# criteria.id_finder
#
# @return [ Symbol ] The name of the finder method.
def id_finder
@id_finder ||= extract_id ? :all_of : :where
end
# Get documents from the database only.
#
# @api private
#
# @example Get documents from the database.
# criteria.from_database(ids)
#
# @param [ Array<Object> ] ids The ids to fetch with.
#
# @return [ Array<Document> ] The matching documents.
def from_database(ids)
from_database_selector(ids).entries
end
def from_database_selector(ids)
if ids.size > 1
any_in(_id: ids)
else
where(_id: ids.first)
end
end
# Convert all the ids to their proper types.
#
# @api private
#
# @example Convert the ids.
# criteria.mongoize_ids(ids)
#
# @param [ Array<Object> ] ids The ids to convert.
#
# @return [ Array<Object> ] The converted ids.
def mongoize_ids(ids)
ids.map do |id|
id = id[:_id] if id.respond_to?(:keys) && id[:_id]
klass.fields["_id"].mongoize(id)
end
end
# Convert args to the +#find+ method into a flat array of ids.
#
# @example Get the ids.
# prepare_ids_for_find([ 1, [ 2, 3 ] ])
#
# @param [ Array<Object> ] args The arguments.
#
# @return [ Array ] The array of ids.
def prepare_ids_for_find(args)
args.flat_map do |arg|
case arg
when Array, Set
prepare_ids_for_find(arg)
when Range
arg.begin&.numeric? && arg.end&.numeric? ? arg.to_a : arg
else
arg
end
end.uniq(&:to_s)
end
# Indicates whether the given arguments array is a list of values.
# Used by the +find+ method to determine whether to return an array
# or single value.
#
# @example Are these arguments a list of values?
# multi_args?([ 1, 2, 3 ]) #=> true
#
# @param [ Array ] args The arguments.
#
# @return [ true | false ] Whether the arguments are a list.
def multi_args?(args)
args.size > 1 || !args.first.is_a?(Hash) && args.first.resizable?
end
# Convenience method of raising an invalid options error.
#
# @example Raise the error.
# criteria.raise_invalid
#
# @raise [ Errors::InvalidOptions ] The error.
def raise_invalid
raise Errors::InvalidFind.new
end
end
end
end