mongomatic 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/mongomatic/base.rb +32 -14
  2. data/test/helper.rb +2 -0
  3. metadata +3 -3
@@ -63,9 +63,19 @@ module Mongomatic
63
63
  end
64
64
 
65
65
  def drop
66
- self.send(:before_drop) if self.respond_to?(:before_drop)
66
+ do_callback(:before_drop)
67
67
  collection.drop
68
- self.send(:after_drop) if self.respond_to?(:after_drop)
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
- self.send(:before_validate) if self.respond_to?(:before_validate)
103
+ do_callback(:before_validate)
94
104
  validate
95
- self.send(:after_validate) if self.respond_to?(:after_validate)
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
- self.send(:before_insert) if self.respond_to?(:before_insert)
152
- self.send(:before_insert_or_update) if self.respond_to?(:before_insert_or_update)
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
- self.send(:after_insert) if self.respond_to?(:after_insert)
158
- self.send(:after_insert_or_update) if self.respond_to?(:after_insert_or_update)
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
- self.send(:before_update) if self.respond_to?(:before_update)
175
- self.send(:before_insert_or_update) if self.respond_to?(:before_insert_or_update)
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
- self.send(:after_update) if self.respond_to?(:after_update)
178
- self.send(:after_insert_or_update) if self.respond_to?(:after_insert_or_update)
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
- self.send(:before_remove) if self.respond_to?(:before_remove)
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
- self.send(:after_remove) if self.respond_to?(:after_remove)
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
@@ -34,6 +34,8 @@ class Person < Mongomatic::Base
34
34
  self.errors << ["Name", "can't be empty"] if self["name"].blank?
35
35
  end
36
36
 
37
+ private
38
+
37
39
  def before_validate
38
40
  self.callback_tests ||= []
39
41
  self.callback_tests << :before_validate
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 1
9
- version: 0.5.1
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-02 00:00:00 -07:00
17
+ date: 2010-09-04 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency