Module: CacheD8a

Defined in:
lib/d8a/cached8a.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject

<d8m> -> { <attr> => <value>… }



5
6
7
# File 'lib/d8a/cached8a.rb', line 5

def cache
  @cache
end

Class Method Details

.extended(d8a) ⇒ Object



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/d8a/cached8a.rb', line 8

def CacheD8a.extended(d8a)
  # Set up new attributes
  #d8a.attrs.push(:cached)

  # load existing .d8acache 
  begin
    yaml = ""
    d8a.read(".d8acache") { |f|
      while s = f.read(10000)
        yaml += s
      end
    }
    d8a.cache = YAML.load(yaml)
  rescue Errno::ENOENT
    d8a.cache = {}
  end


  # wrap methods
  class << d8a
    # use cached attrs when possible
    alias_method :__cached8a_ref, :[]
    def [](d8m, attr = nil)
      cached = cache_entry(d8m)

      if cached
        attr ? cached[attr] : cached

      elsif attr
        __cached8a_ref(d8m, attr)

      elsif d8m =~ /.d8acache$/
        __cached8a_ref(d8m)

      else
        @cache[d8m] = __cached8a_ref(d8m)
      end
    end


    # update cache entry after []=
    alias_method :__cached8a_refeq, :[]=
    def []=(d8m, attr, val = nil)
      result = __cached8a_refeq(d8m, attr, val)
      @cache[d8m][attr] = val if @cache.has_key?(d8m) && ! attr.is_a?(Hash)
      result
    end


    # don't show .d8acache on each
    alias_method :__cached8a_each, :each
    def each
      __cached8a_each do |d8m|
        yield d8m unless d8m =~ /.d8acache$/
      end
    end


    # refresh cache entry after write
    alias_method :__cached8a_write, :write
    def write(d8m, &block)
      @cache.delete(d8m)
      result = __cached8a_write(d8m, &block)
      self[d8m]   # re-cache values
      result
    end


    # clear cache entry after delete
    alias_method :__cached8a_delete, :delete
    def delete(d8m)
      @cache.delete(d8m)
      __cached8a_delete(d8m)
    end


    # rewrite .d8acache on flush
    alias_method :__cached8a_flush, :flush
    def flush
      __cached8a_write(".d8acache") do |f|
        f.write(YAML.dump(@cache))
      end
      __cached8a_flush
    end
  end
end

Instance Method Details

#synthetic_attr(*new_attrs) ⇒ Object

defines a new synthetic attribute that exists only in the cache



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/d8a/cached8a.rb', line 107

def synthetic_attr(*new_attrs)
  new_attrs.each do |new_attr|
    instance_eval %Q!
      class << self
        def #{new_attr.to_s}(d8m, d8m_attrs = nil)
          cached = cache_entry(d8m)
          cached ? cached[#{new_attr.to_s}] : nil
        end

        def #{new_attr.to_s}=(d8m, val)
          self[d8m]   # populate cache
          @cache[d8m][#{new_attr.inspect}] = val
        end
      end!
    @attrs.push(new_attr)
  end
end