Module: Cache::Object::ActiveRecord::ClassMethods

Defined in:
lib/cache/object/active_record.rb

Instance Method Summary collapse

Instance Method Details

#_load(args) ⇒ Object



52
53
54
55
56
57
# File 'lib/cache/object/active_record.rb', line 52

def _load(args)
  attributes = Marshal.load(args)
  object = allocate
  object.load_from_cache(attributes)
  object
end

#fetch_all(ids) ⇒ Object



71
72
73
# File 'lib/cache/object/active_record.rb', line 71

def fetch_all(ids)
  Cache::Object::MultiGet.new(self).fetch_all(ids)
end

#find(*args) ⇒ Object



59
60
61
62
63
# File 'lib/cache/object/active_record.rb', line 59

def find(*args)
  Cache::Object.adapter.fetch(self, *args[0]) do
    super(*args)
  end
end

#find_by_id(id) ⇒ Object



65
66
67
68
69
# File 'lib/cache/object/active_record.rb', line 65

def find_by_id(id)
  Cache::Object.adapter.fetch(self, id) do
    where(self.primary_key => id).first
  end
end

#object_cache_on(*attrs) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/cache/object/active_record.rb', line 75

def object_cache_on(*attrs)
  self._object_cache_attr_mappings << attrs
  define_singleton_method("find_by_#{attrs.join('_and_')}") do |*args|
    attributes = Hash[attrs.zip(args)]
    Cache::Object.adapter.fetch_mapping(self, attributes) do
      super(*args)
    end
  end
end