Sha256: 10a30e7e4d1c5d51a4a190153d16ee242c8ac90fbd8389882ad3d47bff55376b

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 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#{Regexp.quote(name)}/} and __mize_cache__.delete(n)
    end
    self
  end

  private

  # Set the cache object to +cache+.
  def __mize_cache__=(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

3 entries across 3 versions & 1 rubygems

Version Path
mize-0.6.1 lib/mize/cache_methods.rb
mize-0.6.0 lib/mize/cache_methods.rb
mize-0.5.0 lib/mize/cache_methods.rb