/
querying.rb
176 lines (143 loc) · 4.67 KB
/
querying.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
# encoding: UTF-8
require 'mongo_mapper/plugins/querying/decorator'
require 'mongo_mapper/plugins/querying/plucky_methods'
module MongoMapper
module Plugins
module Querying
module ClassMethods
include PluckyMethods
def find_each(opts={})
super(opts).each { |doc| yield load(doc) }
end
def find_by_id(id)
find_one(:_id => id)
end
def first_or_create(args)
first(args) || create(args.reject { |key, value| !key?(key) })
end
def first_or_new(args)
first(args) || new(args.reject { |key, value| !key?(key) })
end
def create(*docs)
initialize_each(*docs) { |doc| doc.save }
end
def create!(*docs)
initialize_each(*docs) { |doc| doc.save! }
end
def update(*args)
if args.length == 1
update_multiple(args[0])
else
id, attributes = args
update_single(id, attributes)
end
end
def delete(*ids)
query(:_id => ids.flatten).remove
end
def delete_all(options={})
query(options).remove
end
def destroy(*ids)
find_some!(ids.flatten).each(&:destroy)
end
def destroy_all(options={})
find_each(options) { |document| document.destroy }
end
# @api private for now
def query(options={})
Plucky::Query.new(collection).tap do |query|
query.extend(Decorator)
query.object_ids(object_id_keys)
query.update(options)
query.model(self)
end
end
# @api private for now
def criteria_hash(criteria={})
Plucky::CriteriaHash.new(criteria, :object_ids => object_id_keys)
end
private
def find_some(ids, options={})
query = query(options).update(:_id => ids.flatten.compact.uniq)
find_many(query.to_hash).compact
end
def find_some!(ids, options={})
ids = ids.flatten.compact.uniq
docs = find_some(ids, options)
if ids.size != docs.size
raise DocumentNotFound, "Couldn't find all of the ids (#{ids.to_sentence}). Found #{docs.size}, but was expecting #{ids.size}"
end
docs
end
# All query methods that load documents pass through find_one or find_many
def find_one(options={})
query(options).first
end
# All query methods that load documents pass through find_one or find_many
def find_many(options)
query(options).all
end
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 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
find(id).tap do |doc|
doc.update_attributes(attrs)
end
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 save(options={})
options.assert_valid_keys(:validate, :safe)
options.reverse_merge!(:validate => true)
!options[:validate] || valid? ? create_or_update(options) : false
end
def save!(options={})
options.assert_valid_keys(:safe)
save(options) || raise(DocumentNotValid.new(self))
end
def destroy
delete
end
def delete
@_destroyed = true
self.class.delete(id) unless new?
end
private
def create_or_update(options={})
result = new? ? create(options) : update(options)
result != false
end
def create(options={})
save_to_collection(options)
end
def update(options={})
save_to_collection(options)
end
def save_to_collection(options={})
@_new = false
collection.save(to_mongo, :safe => options[:safe])
end
end
end
end
end