Module: Cache::Object::ActiveRecord

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cache/object/active_record.rb', line 6

def self.included(base)
  base.instance_eval do
    extend ClassMethods

    def _object_cache_attr_mappings
      @_object_cache_attr_mappings ||= []
    end

    def _object_cache_include
      @_object_cache_include ||= []
    end

    after_destroy :expire_cache!
    after_save :write_cache!
    after_rollback :expire_cache!
  end
end

Instance Method Details

#_cache_object_decoratorObject



58
59
60
# File 'lib/cache/object/active_record.rb', line 58

def _cache_object_decorator
  Cache::Object::InstanceDecorator.new(self, self.class._object_cache_attr_mappings)
end

#_dump(level = 0) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/cache/object/active_record.rb', line 24

def _dump(level = 0)
  additional_attributes = {}.tap do |h|
    self.class._object_cache_include.flatten.each do |key|
      h[key.to_s] = self.send(key)
    end
  end
  Marshal.dump(attributes.merge(additional_attributes))
end

#expire_cache!Object



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

def expire_cache!
  return unless self.send(self.class.primary_key)
  Cache::Object.adapter.delete(_cache_object_decorator)
end

#load_from_cache(attributes) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cache/object/active_record.rb', line 33

def load_from_cache(attributes)
  self.class._object_cache_include.flatten.each do |key|
    send("#{key}=", attributes.delete(key.to_s))
  end

  _assign_attributes_from_cache(attributes)
  @relation = nil

  _initialize_from_cache
  @new_record = false
  @column_types = self.class.column_types if self.class.respond_to?(:column_types)
  @changed_attributes = {}
  @new_record = false
end

#write_cache!Object



48
49
50
51
# File 'lib/cache/object/active_record.rb', line 48

def write_cache!
  return unless self.send(self.class.primary_key)
  Cache::Object.adapter.write(_cache_object_decorator)
end