Module: Cacheable::Keys::InstanceKeys

Defined in:
lib/cacheable/keys.rb

Instance Method Summary collapse

Instance Method Details

#base_class_or_name(name) ⇒ Object

If it isa class. It should be the base_class name else it should just be a name tableized



64
65
66
67
68
69
70
71
# File 'lib/cacheable/keys.rb', line 64

def base_class_or_name(name)
  name = begin
    name.capitalize.constantize.base_class.name
  rescue NameError # uninitialized constant
    name
  end
  name.tableize
end

#belong_association_cache_key(name, polymorphic = nil) ⇒ Object

Returns nil if association cannot be qualified



47
48
49
50
51
52
53
54
55
56
# File 'lib/cacheable/keys.rb', line 47

def belong_association_cache_key(name, polymorphic=nil)
  name = name.to_s if name.is_a?(Symbol)

  if polymorphic && self.respond_to?(:"#{name}_type")
    return nil unless self.send(:"#{name}_type").present?
    "#{base_class_or_name(self.send(:"#{name}_type"))}/#{self.send(:"#{name}_id")}"
  else
    "#{base_class_or_name(name)}/#{self.send(:"#{name}_id")}"
  end
end

#have_association_cache_key(name) ⇒ Object



58
59
60
# File 'lib/cacheable/keys.rb', line 58

def have_association_cache_key(name)
  "#{model_cache_key}/association/#{name}"
end

#method_cache_key(meth) ⇒ Object



42
43
44
# File 'lib/cacheable/keys.rb', line 42

def method_cache_key(meth)
  "#{model_cache_key}/method/#{meth}"
end

#model_cache_keyObject



38
39
40
# File 'lib/cacheable/keys.rb', line 38

def model_cache_key
  "#{self.class.base_class.name.tableize}/#{self.id.to_i}"
end

#model_cache_keysObject



34
35
36
# File 'lib/cacheable/keys.rb', line 34

def model_cache_keys
  ["#{self.class.base_class.name.tableize}/#{self.id.to_i}", "#{self.class.base_class.name.tableize}/#{self.to_param}"]
end