module Memoizable

Memoizable code for caching link_to objects see snippets.dzone.com/posts/show/5300

Constants

CACHE

Store for cached values.

Public Class Methods

flush_memos() click to toggle source

Flush cached return values.

# File lib/sinatra_fedora/memoize.rb, line 24
def flush_memos
  CACHE.clear
end

Public Instance Methods

memoize(*names) click to toggle source

Memoize the given method(s).

# File lib/sinatra_fedora/memoize.rb, line 8
def memoize(*names)
  names.each do |name|
    unmemoized = "__unmemoized_#{name}"
    
    class_eval %Q{
      alias   :#{unmemoized} :#{name}
      private :#{unmemoized}
      def #{name}(*args)
        cache = CACHE[self][#{name.inspect}]
        cache.has_key?(args) ? cache[args] : (cache[args] = send(:#{unmemoized}, *args))
      end
    }
  end
end

Private Instance Methods

flush_memos() click to toggle source

Flush cached return values.

# File lib/sinatra_fedora/memoize.rb, line 24
def flush_memos
  CACHE.clear
end