forked from mongomapper/mongomapper
/
document.rb
357 lines (297 loc) · 9.37 KB
/
document.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
require 'set'
module MongoMapper
module Document
def self.included(model)
model.class_eval do
include EmbeddedDocument
include InstanceMethods
include Observing
include Callbacks
include Dirty
include RailsCompatibility::Document
extend Validations::Macros
extend ClassMethods
extend Finders
def self.per_page
25
end unless respond_to?(:per_page)
end
descendants << model
end
def self.descendants
@descendants ||= Set.new
end
module ClassMethods
def key(*args)
key = super
create_indexes_for(key)
key
end
def ensure_index(name_or_array, options={})
keys_to_index = if name_or_array.is_a?(Array)
name_or_array.map { |pair| [pair[0], pair[1]] }
else
name_or_array
end
MongoMapper.ensure_index(self, keys_to_index, options)
end
def find(*args)
options = args.extract_options!
case args.first
when :first then first(options)
when :last then last(options)
when :all then find_every(options)
when Array then find_some(args, options)
else
case args.size
when 0
raise DocumentNotFound, "Couldn't find without an ID"
when 1
find_one(args[0], options)
else
find_some(args, options)
end
end
end
def paginate(options)
per_page = options.delete(:per_page) || self.per_page
page = options.delete(:page)
total_entries = count(options[:conditions] || {})
collection = Pagination::PaginationProxy.new(total_entries, page, per_page)
options[:limit] = collection.limit
options[:skip] = collection.skip
collection.subject = find_every(options)
collection
end
def first(options={})
options.merge!(:limit => 1)
find_every(options)[0]
end
def last(options={})
if options[:order].blank?
raise ':order option must be provided when using last'
end
options.merge!(:limit => 1)
options[:order] = invert_order_clause(options[:order])
find_every(options)[0]
end
def all(options={})
find_every(options)
end
def find_by_id(id)
criteria = FinderOptions.to_mongo_criteria(self, :_id => id)
if doc = collection.find_one(criteria)
new(doc)
end
end
def count(conditions={})
collection.find(FinderOptions.to_mongo_criteria(self, conditions)).count
end
def exists?(conditions={})
!count(conditions).zero?
end
def create(*docs)
initialize_each(*docs) { |doc| doc.save }
end
def create!(*docs)
initialize_each(*docs) { |doc| doc.save! }
end
# For updating single document
# Person.update(1, {:foo => 'bar'})
#
# For updating multiple documents at once:
# Person.update({'1' => {:foo => 'bar'}, '2' => {:baz => 'wick'}})
def update(*args)
updating_multiple = args.length == 1
if updating_multiple
update_multiple(args[0])
else
id, attributes = args
update_single(id, attributes)
end
end
def delete(*ids)
criteria = FinderOptions.to_mongo_criteria(self, :_id => ids.flatten)
collection.remove(criteria)
end
def delete_all(conditions={})
criteria = FinderOptions.to_mongo_criteria(self, conditions)
collection.remove(criteria)
end
def destroy(*ids)
find_some(ids.flatten).each(&:destroy)
end
def destroy_all(conditions={})
all(conditions).each(&:destroy)
end
def connection(mongo_connection=nil)
if mongo_connection.nil?
@connection ||= MongoMapper.connection
else
@connection = mongo_connection
end
@connection
end
def database(name=nil)
if name.nil?
@database ||= MongoMapper.database
else
@database = connection.db(name)
end
@database
end
# Changes the collection name from the default to whatever you want
def set_collection_name(name=nil)
@collection = nil
@collection_name = name
end
# Returns the collection name, if not set, defaults to class name tableized
def collection_name
@collection_name ||= self.to_s.demodulize.tableize
end
# Returns the mongo ruby driver collection object
def collection
@collection ||= database.collection(collection_name)
end
def timestamps!
key :created_at, Time
key :updated_at, Time
class_eval { before_save :update_timestamps }
end
def single_collection_inherited?
keys.has_key?('_type') && single_collection_inherited_superclass?
end
def single_collection_inherited_superclass?
superclass.respond_to?(:keys) && superclass.keys.has_key?('_type')
end
protected
def method_missing(method, *args)
finder = DynamicFinder.new(method)
if finder.found?
meta_def(finder.method) { |*args| dynamic_find(finder, args) }
send(finder.method, *args)
else
super
end
end
private
# Initializes each document and yields each initialized document
def initialize_each(*docs)
instances = []
docs = [{}] if docs.blank?
docs.flatten.each do |attrs|
doc = new(attrs)
yield(doc)
instances << doc
end
instances.size == 1 ? instances[0] : instances
end
def create_indexes_for(key)
ensure_index key.name if key.options[:index]
end
def find_every(options)
criteria, options = FinderOptions.new(self, options).to_a
collection.find(criteria, options).to_a.map do |doc|
begin
klass = doc['_type'].present? ? doc['_type'].constantize : self
klass.new(doc)
rescue NameError
new(doc)
end
end
end
def invert_order_clause(order)
order.split(',').map do |order_segment|
if order_segment =~ /\sasc/i
order_segment.sub /\sasc/i, ' desc'
elsif order_segment =~ /\sdesc/i
order_segment.sub /\sdesc/i, ' asc'
else
"#{order_segment.strip} desc"
end
end.join(',')
end
def find_some(ids, options={})
ids = ids.flatten.compact.uniq
documents = find_every(options.deep_merge(:conditions => {'_id' => ids}))
if ids.size == documents.size
documents
else
raise DocumentNotFound, "Couldn't find all of the ids (#{ids.to_sentence}). Found #{documents.size}, but was expecting #{ids.size}"
end
end
def find_one(id, options={})
if doc = find_every(options.deep_merge(:conditions => {:_id => id})).first
doc
else
raise DocumentNotFound, "Document with id of #{id} does not exist in collection named #{collection.name}"
end
end
def update_single(id, attrs)
if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
end
doc = find(id)
doc.update_attributes(attrs)
doc
end
def update_multiple(docs)
unless docs.is_a?(Hash)
raise ArgumentError, "Updating multiple documents takes 1 argument and it must be hash"
end
instances = []
docs.each_pair { |id, attrs| instances << update(id, attrs) }
instances
end
end
module InstanceMethods
def collection
self.class.collection
end
def new?
read_attribute('_id').blank? || using_custom_id?
end
def save
valid? ? create_or_update : false
end
def save!
valid? ? create_or_update : raise(DocumentNotValid.new(self))
end
def destroy
return false if frozen?
criteria = FinderOptions.to_mongo_criteria(self.class, :_id => id)
collection.remove(criteria) unless new?
freeze
end
private
def create_or_update
result = new? ? create : update
result != false
end
def create
assign_id
save_to_collection
end
def assign_id
if read_attribute(:_id).blank?
write_attribute(:_id, Mongo::ObjectID.new.to_s)
end
end
def update
save_to_collection
end
def save_to_collection
clear_custom_id_flag
collection.save(to_mongo)
end
def update_timestamps
now = Time.now.utc
write_attribute('created_at', now) if new?
write_attribute('updated_at', now)
end
def clear_custom_id_flag
@using_custom_id = nil
end
end
end # Document
end # MongoMapper