Module: DirtyMemoize::ClassMethods

Defined in:
lib/dirty-memoize.rb

Instance Method Summary collapse

Instance Method Details

#dirty_memoize(*dependent) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dirty-memoize.rb', line 50

def dirty_memoize(*dependent)
  dependent.each do |sym|
    alias_method((sym.to_s+"_without_dirty").intern, sym)
    define_method(sym) do |*args|
      if(dirty?)
        clean_cache
        if self.class.const_defined? "DIRTY_COMPUTE"
          send(self.class.const_get("DIRTY_COMPUTE"))
        else
          send(:compute)
        end
        @compute_count||=0
        @compute_count+=1
        @dirty=:false
      end
      @cache[sym]||=Hash.new
      @cache[sym][args]||=send(sym.to_s+"_without_dirty", *args)
    end
  end
end

#dirty_writer(*independent) ⇒ Object

Set variables which



39
40
41
42
43
44
45
46
47
48
# File 'lib/dirty-memoize.rb', line 39

def dirty_writer(*independent)
  independent.each do |sym|
    sym=sym.to_s+"="
    alias_method((sym.to_s+"_without_dirty").intern, sym)
    define_method(sym) do |*args|
      @dirty=:true
      send(sym.to_s+"_without_dirty", *args)
    end
  end
end