module Releaf::InstanceCache

Public Class Methods

cache_instance_method(method_name) click to toggle source
# File lib/releaf/instance_cache.rb, line 35
def self.cache_instance_method method_name
  if instance_methods.include?(method_name)
    # method already defined, alias it
    create_instance_cache_method_alias method_name
  else
    # method not defined yet, add to queue
    instance_methods_to_cache << method_name
  end
end
cache_instance_methods(*method_names) click to toggle source
# File lib/releaf/instance_cache.rb, line 31
def self.cache_instance_methods(*method_names)
  method_names.each { |method_name| cache_instance_method(method_name) }
end
create_instance_cache_method_alias(method_name) click to toggle source
# File lib/releaf/instance_cache.rb, line 45
def self.create_instance_cache_method_alias method_name
  original_method_name = instance_cache_original_method_name( method_name )
  alias_method original_method_name, method_name
  define_method(method_name) do
    instance_cache(method_name) do
      send(original_method_name)
    end
  end
  self.cached_instance_methods << method_name
end
instance_cache_original_method_name(method_name) click to toggle source
# File lib/releaf/instance_cache.rb, line 10
def self.instance_cache_original_method_name(method_name)
  "instance_cache_original_#{method_name}"
end
method_added(method_name) click to toggle source
# File lib/releaf/instance_cache.rb, line 19
def self.method_added(method_name)
  # call previous definition of method_added if present
  parent_method_added = instance_cache_original_method_name(:method_added)
  send(parent_method_added, method_name) if respond_to?(parent_method_added)

  # see if the newly added method is in the queue needing to be cached
  method_needs_to_be_cached = instance_methods_to_cache.delete(method_name)
  if method_needs_to_be_cached.present?
    create_instance_cache_method_alias(method_name)
  end
end

Public Instance Methods

instance_cache(key) { || ... } click to toggle source
# File lib/releaf/instance_cache.rb, line 65
def instance_cache(key)
  if instance_cache_store.key?(key)
    instance_cache_store[key]
  else
    instance_cache_store[key] = yield
  end
end
instance_cache_store() click to toggle source
# File lib/releaf/instance_cache.rb, line 57
def instance_cache_store
  @instance_cache_store ||= {}
end
reset_instance_cache() click to toggle source
# File lib/releaf/instance_cache.rb, line 61
def reset_instance_cache
  @instance_cache_store = {}
end