Module: Dropbox::Memoization::ClassMethods

Defined in:
lib/dropbox/memoization.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#memoize(*method_names) ⇒ Object

:nodoc:


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dropbox/memoization.rb', line 82

def memoize(*method_names) # :nodoc:
  method_names.each do |meth|
    define_method :"#{meth}_with_memo" do |*args|
      if @_memoize then
        identifier = Digest::SHA1.hexdigest(meth.to_s + ":" + args.to_yaml)
        @_memo_identifiers << identifier
        (@_memo_cache_proc || Proc.new { |ident, calculate_proc| eval "@_memo_#{ident} ||= calculate_proc.call" }).call identifier, Proc.new { send :"#{meth}_without_memo", *args }
      else
        send :"#{meth}_without_memo", *args
      end
    end
    alias_method_chain meth, :memo
  end
end