Module: ErectorCache::AbstractWidget::ClassMethods

Defined in:
lib/erector_cache/abstract_widget.rb

Instance Method Summary collapse

Instance Method Details

#cache_for(ttl) ⇒ Object



23
24
25
# File 'lib/erector_cache/abstract_widget.rb', line 23

def cache_for(ttl)
  self.expire_in = ttl
end

#cache_key(hash, wildcard = false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/erector_cache/abstract_widget.rb', line 33

def cache_key(hash, wildcard=false)
  self.key_components.inject([self.to_s]) do |collection, part|
    key = (part.is_a?(Hash) ? part.keys.first : part)
    object = hash[key]

    if wildcard && object == "*"
      value = "*"
    elsif part.is_a?(Hash)
      value = Array(part[key]).map do |p| 
        if p.is_a?(Proc)
          p.call(object)
        else
          object.send(p)
        end
      end.join("-")
    else
      value = object.to_param
    end
    collection << [key, value]
  end.flatten.join(":")
end

#cache_with(*components) ⇒ Object



15
16
17
# File 'lib/erector_cache/abstract_widget.rb', line 15

def cache_with(*components)
  self.key_components = components
end

#expire!(hash = {}) ⇒ Object



27
28
29
30
31
# File 'lib/erector_cache/abstract_widget.rb', line 27

def expire!(hash={})
  hash = Hash.new("*").merge(hash)
  search_key = "Lawnchair:"+cache_key(hash, true)
  LAWNCHAIR.redis.keys(search_key).each{|key| LAWNCHAIR.redis.del(key) }
end

#interpolate(interpolations) ⇒ Object



19
20
21
# File 'lib/erector_cache/abstract_widget.rb', line 19

def interpolate(interpolations)
  self.interpolations = interpolations
end