class CacheRecord

Public Class Methods

clear() click to toggle source
# File lib/cache_driver/cache_record.rb, line 22
def self.clear
  self.find_all.each(&:destroy)
end
find_all() click to toggle source
# File lib/cache_driver/cache_record.rb, line 6
def self.find_all
  CacheRecord.cache_util.read_all CacheUtil.class_to_type(self)
end
find_by_key(key) click to toggle source
# File lib/cache_driver/cache_record.rb, line 10
def self.find_by_key(key)
  CacheRecord.cache_util.read CacheUtil.class_to_type(self), key
end
find_current() click to toggle source
# File lib/cache_driver/cache_record.rb, line 14
def self.find_current
  if self.key_attr
    raise "#{self} is not a unique class, use method find_by_key instead"
  else
    self.find_by_key "current"
  end
end
from_cache(obj) click to toggle source
# File lib/cache_driver/cache_record.rb, line 47
def self.from_cache(obj)
  ins = self.allocate
  obj.each do |key, value|
    ins.instance_variable_set "@#{key}", value
  end
  ins
end
key_attr() click to toggle source
# File lib/cache_driver/cache_record.rb, line 2
def self.key_attr
  nil
end

Private Class Methods

cache_util() click to toggle source
# File lib/cache_driver/cache_record.rb, line 56
def self.cache_util
  CacheDriver.store_file? ? FileCacheUtil : CacheDriver.store_redis? ? RedisCacheUtil : nil
end

Public Instance Methods

destroy() click to toggle source
# File lib/cache_driver/cache_record.rb, line 32
def destroy
  raise "attr key :#{self.class.key_attr} is missing" if self.key_attr_missing?

  CacheRecord.cache_util.delete CacheUtil.class_to_type(self.class), self.class.key_attr ? self.send(self.class.key_attr) : "current"
end
save() click to toggle source
# File lib/cache_driver/cache_record.rb, line 26
def save
  raise "attr key :#{self.class.key_attr} is missing" if self.key_attr_missing?

  CacheRecord.cache_util.write CacheUtil.class_to_type(self.class), self.class.key_attr ? self.send(self.class.key_attr) : "current", self
end
to_cache() click to toggle source
# File lib/cache_driver/cache_record.rb, line 38
def to_cache
  res = {}
  self.instance_variables.each do |var|
    var_name = var.to_s.delete('@')
    res[var_name.to_sym] = self.instance_variable_get("@#{var_name}")
  end
  res
end

Protected Instance Methods

key_attr_missing?() click to toggle source
# File lib/cache_driver/cache_record.rb, line 61
def key_attr_missing?
  self.class.key_attr && self.instance_variable_get("@#{self.class.key_attr}").nil?
end