mongomatic 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongomatic/base.rb +32 -14
- data/test/helper.rb +2 -0
- metadata +3 -3
data/lib/mongomatic/base.rb
CHANGED
@@ -63,9 +63,19 @@ module Mongomatic
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def drop
|
66
|
-
|
66
|
+
do_callback(:before_drop)
|
67
67
|
collection.drop
|
68
|
-
|
68
|
+
do_callback(:after_drop)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def do_callback(meth)
|
74
|
+
begin
|
75
|
+
send(meth)
|
76
|
+
rescue NoMethodError => e
|
77
|
+
false
|
78
|
+
end
|
69
79
|
end
|
70
80
|
end
|
71
81
|
|
@@ -90,9 +100,9 @@ module Mongomatic
|
|
90
100
|
|
91
101
|
def valid?
|
92
102
|
self.errors = Mongomatic::Errors.new
|
93
|
-
|
103
|
+
do_callback(:before_validate)
|
94
104
|
validate
|
95
|
-
|
105
|
+
do_callback(:after_validate)
|
96
106
|
self.errors.empty?
|
97
107
|
end
|
98
108
|
|
@@ -148,14 +158,14 @@ module Mongomatic
|
|
148
158
|
# require an additional call to the db.
|
149
159
|
def insert(opts={})
|
150
160
|
return false unless new? && valid?
|
151
|
-
|
152
|
-
|
161
|
+
do_callback(:before_insert)
|
162
|
+
do_callback(:before_insert_or_update)
|
153
163
|
if ret = self.class.collection.insert(@doc,opts)
|
154
164
|
@doc["_id"] = @doc.delete(:_id) if @doc[:_id]
|
155
165
|
self.is_new = false
|
156
166
|
end
|
157
|
-
|
158
|
-
|
167
|
+
do_callback(:after_insert)
|
168
|
+
do_callback(:after_insert_or_update)
|
159
169
|
ret
|
160
170
|
end
|
161
171
|
|
@@ -171,11 +181,11 @@ module Mongomatic
|
|
171
181
|
# error raised. Note that this will require an additional call to the db.
|
172
182
|
def update(opts={},update_doc=@doc)
|
173
183
|
return false if new? || removed? || !valid?
|
174
|
-
|
175
|
-
|
184
|
+
do_callback(:before_update)
|
185
|
+
do_callback(:before_insert_or_update)
|
176
186
|
ret = self.class.collection.update({"_id" => @doc["_id"]}, update_doc, opts)
|
177
|
-
|
178
|
-
|
187
|
+
do_callback(:after_update)
|
188
|
+
do_callback(:after_insert_or_update)
|
179
189
|
ret
|
180
190
|
end
|
181
191
|
|
@@ -188,11 +198,11 @@ module Mongomatic
|
|
188
198
|
# if you want an exception raised.
|
189
199
|
def remove(opts={})
|
190
200
|
return false if new?
|
191
|
-
|
201
|
+
do_callback(:before_remove)
|
192
202
|
if ret = self.class.collection.remove({"_id" => @doc["_id"]})
|
193
203
|
self.removed = true; freeze; ret
|
194
204
|
end
|
195
|
-
|
205
|
+
do_callback(:after_remove)
|
196
206
|
ret
|
197
207
|
end
|
198
208
|
|
@@ -209,6 +219,14 @@ module Mongomatic
|
|
209
219
|
|
210
220
|
protected
|
211
221
|
|
222
|
+
def do_callback(meth)
|
223
|
+
begin
|
224
|
+
send(meth)
|
225
|
+
rescue NoMethodError => e
|
226
|
+
false
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
212
230
|
def doc
|
213
231
|
@doc
|
214
232
|
end
|
data/test/helper.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 2
|
9
|
+
version: 0.5.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben Myles
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-04 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|