Class: Wakame::StatusDB::Model
- Inherits:
-
Object
- Object
- Wakame::StatusDB::Model
show all
- Includes:
- AttributeHelper
- Defined in:
- lib/wakame/status_db.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary
AttributeHelper::CLASS_TYPE_KEY, AttributeHelper::CONVERT_CLASSES, AttributeHelper::PRIMITIVE_CLASSES
Class Method Summary
collapse
Instance Method Summary
collapse
#dump_attrs, #retrieve_attr_attribute
Class Method Details
.inherited(klass) ⇒ Object
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/wakame/status_db.rb', line 257
def self.inherited(klass)
klass.extend(ClassMethods)
klass.class_eval {
attr_attributes[:id]={:persistent=>false}
}
end
|
Instance Method Details
#delete ⇒ Object
307
308
309
|
# File 'lib/wakame/status_db.rb', line 307
def delete
self.class.delete(self.id)
end
|
#dirty? ⇒ Boolean
276
277
278
|
# File 'lib/wakame/status_db.rb', line 276
def dirty?
raise NotImplementedError
end
|
#id ⇒ Object
268
269
270
|
# File 'lib/wakame/status_db.rb', line 268
def id
@id ||= Wakame::Util.gen_id
end
|
#new_record? ⇒ Boolean
272
273
274
|
# File 'lib/wakame/status_db.rb', line 272
def new_record?
@load_at.nil?
end
|
#on_after_delete ⇒ Object
350
351
|
# File 'lib/wakame/status_db.rb', line 350
def on_after_delete
end
|
#on_after_load ⇒ Object
Called after copying data from database in self.find().
344
345
|
# File 'lib/wakame/status_db.rb', line 344
def on_after_load
end
|
#on_before_delete ⇒ Object
347
348
|
# File 'lib/wakame/status_db.rb', line 347
def on_before_delete
end
|
#on_before_load ⇒ Object
Called prior to copying data from database in self.find().
341
342
|
# File 'lib/wakame/status_db.rb', line 341
def on_before_load
end
|
#reload ⇒ Object
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
|
# File 'lib/wakame/status_db.rb', line 311
def reload
self.class._instance_cache.delete(self.id)
hash = StatusDB.barrier {
StatusDB.adapter.find(self.id)
}
if hash[AttributeHelper::CLASS_TYPE_KEY]
klass_const = Util.build_const(hash[AttributeHelper::CLASS_TYPE_KEY])
else
klass_const = self.class
end
unless klass_const <= self.class
raise "The class \"#{klass_const.to_s}\" has no relationship to #{self.class}"
end
on_before_load
@load_at = Time.now
hash.each { |k,v|
instance_variable_set("@#{k}", v)
}
on_after_load
end
|
#save ⇒ Object
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
|
# File 'lib/wakame/status_db.rb', line 280
def save
validate_on_save
self.class.merged_attr_attributes.each { |k,v|
next unless v[:persistent]
if v[:call_after_changed]
case v[:call_after_changed]
when Symbol
self.__send__(v[:call_after_changed].to_sym) when Proc
v[:call_after_changed].call(self)
end
end
}
hash_saved = self.dump_attrs { |k,v,dumper|
if v[:persistent] == true
dumper.call(k)
end
}
StatusDB.barrier {
StatusDB.adapter.save(self.id, hash_saved)
}
end
|