Sha256: 51fd42227c3b4488a620c6d9482bc381b94d9fa511291a80f26b469fdfaba025

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

require 'mize/cache_protocol'
require 'mize/default_cache'

module Mize::CacheMethods
  # Clear cached values for all methods/functions of this object.
  def mize_cache_clear
    __mize_cache__.clear
    self
  end

  # Clear all cached results for the method/function +name+.
  def mize_cache_clear_name(name)
    name = build_key_prefix(name)
    __mize_cache__.each_name do |n|
      n =~ %r{\A#{name}/} and __mize_cache__.delete(n)
    end
    self
  end

  private

  # Set the cache object to +cache+.
  def __mize_cache__=(cache)
    Mize::CacheProtocol.check cache
    @__mize_cache__ = cache
  end

  # Return the cache object.
  def __mize_cache__
    if defined?(@__mize_cache__)
      @__mize_cache__
    else
      self.__mize_cache__ = Mize.default_cache.prototype
    end
  end

  # Build a key prefix for +name+.
  def build_key_prefix(name)
    "mize/#{name}"
  end

  # Build a +name+ prefixed key for the arguments +args+.
  def build_key(name, *args)
    "#{build_key_prefix(name)}/#{Marshal.dump(args)}"
  end

  # Apply the visibility of method +id+ to the wrapper method of this method.
  def memoize_apply_visibility(id)
    visibility = instance_eval do
      case
      when private_method_defined?(id)
        :private
      when protected_method_defined?(id)
        :protected
      end
    end
    yield
  ensure
    visibility and __send__(visibility, id)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mize-0.3.3 lib/mize/cache_methods.rb
mize-0.3.2 lib/mize/cache_methods.rb
mize-0.3.1 lib/mize/cache_methods.rb
mize-0.3.0 lib/mize/cache_methods.rb
mize-0.2.1 lib/mize/cache_methods.rb