Class: Wakame::StatusDB::Model

Inherits:
Object
  • Object
show all
Includes:
AttributeHelper
Defined in:
lib/wakame/status_db.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from AttributeHelper

AttributeHelper::CLASS_TYPE_KEY, AttributeHelper::CONVERT_CLASSES, AttributeHelper::PRIMITIVE_CLASSES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AttributeHelper

#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 {
    #include(::AttributeHelper)
    #enable_cache

    # Manually set attr option to get :id appeared in dump_attrs.
    attr_attributes[:id]={:persistent=>false}
  }
end

Instance Method Details

#deleteObject



307
308
309
# File 'lib/wakame/status_db.rb', line 307

def delete
  self.class.delete(self.id)
end

#dirty?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


276
277
278
# File 'lib/wakame/status_db.rb', line 276

def dirty?
  raise NotImplementedError
end

#idObject



268
269
270
# File 'lib/wakame/status_db.rb', line 268

def id
  @id ||= Wakame::Util.gen_id
end

#new_record?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/wakame/status_db.rb', line 272

def new_record?
  @load_at.nil?
end

#on_after_deleteObject



350
351
# File 'lib/wakame/status_db.rb', line 350

def on_after_delete
end

#on_after_loadObject

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_deleteObject



347
348
# File 'lib/wakame/status_db.rb', line 347

def on_before_delete
end

#on_before_loadObject

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

#reloadObject



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
  
  # klass_const class is equal to self class or child of self class
  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

#saveObject



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
  #return unless dirty?

  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) # if self.respond_to?(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