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



284
285
286
287
288
289
290
291
292
293
# File 'lib/wakame/status_db.rb', line 284

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



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

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

#dirty?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/wakame/status_db.rb', line 303

def dirty?(key=nil)
  return true if new_record?

  if key
    attr_attr = self.class.get_attr_attribute(key.to_sym)
    raise "#{key} is not the key to be saved" if attr_attr.nil? || !attr_attr[:persistent]
    return @_orig[key.to_sym] != self.__send__(key.to_sym)
  else
    self.class.merged_attr_attributes.each { |k,v|
      next unless v[:persistent]
      #p "@_orig[#{k.to_sym}]=#{@_orig[k.to_sym].inspect}"
      #p "@self.__send__(#{k.to_sym})=#{self.__send__(k.to_sym).inspect}"
      return true if @_orig[k.to_sym] != self.__send__(k.to_sym)
    }
    return false
  end
end

#idObject



295
296
297
# File 'lib/wakame/status_db.rb', line 295

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

#new_record?Boolean

Returns:

  • (Boolean)


299
300
301
# File 'lib/wakame/status_db.rb', line 299

def new_record?
  @load_at.nil?
end

#on_after_deleteObject



370
371
# File 'lib/wakame/status_db.rb', line 370

def on_after_delete
end

#on_after_loadObject

Called after copying data from database in self.find().



364
365
# File 'lib/wakame/status_db.rb', line 364

def on_after_load
end

#on_before_deleteObject



367
368
# File 'lib/wakame/status_db.rb', line 367

def on_before_delete
end

#on_before_loadObject

Called prior to copying data from database in self.find().



361
362
# File 'lib/wakame/status_db.rb', line 361

def on_before_load
end

#reloadObject



353
354
355
356
# File 'lib/wakame/status_db.rb', line 353

def reload
  self.class._instance_cache.delete(self.id)
  self.class.find(self.id)
end

#saveObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/wakame/status_db.rb', line 321

def save
#        return unless dirty?
#       raise "No change" unless dirty?

  validate_on_save

  self.class.merged_attr_attributes.each { |k,v|
    next unless v[:persistent]
    if dirty?(k) && 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
  }
  @_orig = hash_saved.dup.freeze

  StatusDB.adapter.save(self.id, hash_saved)
end