public
Rubygem
Description: a humane, eval-safe templating system using Hpricot
Clone URL: git://github.com/mattly/hpreserve.git
Click here to lend your support to: hpreserve and make a donation at www.pledgie.com !
commit  2fe01b8cfeb32b1954550d6c247741b9b716ba57
tree    9699705029c08d590db71c73f752e5c921c9bad2
parent  e74c58b0875f11b60a1c50233895147bc499fb97
hpreserve / lib / hpreserve / abstract_cacher.rb
100644 35 lines (26 sloc) 0.721 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Hpreserve
  class AbstractCacher
    
    attr_accessor :patterns, :storage
    
    def initialize(patterns)
      self.patterns = patterns
    end
    
    def match?(variable)
      pattern = patterns.detect {|p| variable.match(p[:match]) }
      return nil unless pattern
      variable.gsub(pattern[:match], pattern[:key])
    end
    
    
    # overwrite these in your ConcreteCacher to do use whatever you use
    
    def retrieve(key)
      @storage ||= {}
      storage[key]
    end
    
    def store(key, value)
      @storage ||= {}
      storage[key] = value
    end
    
    def expire(pattern)
      @storage ||= {}
      storage.delete_if {|key, value| key.match(pattern) }
    end
    
  end
end