Sha256: 895ceb6a0d1373450324f757f5edba6c256fdbfbd51493e57a69e34dad28d59e

Contents?: true

Size: 582 Bytes

Versions: 3

Compression:

Stored size: 582 Bytes

Contents

##
# Helper module for caching methods inside a class
# To use, extend your class with MethodCacher
# Then, in initialize, call enable_caching

module MethodCacher
  ##
  # Enable caching for the listed methods

  def enable_caching(methods, cache = nil)
    cache ||= BasicCache.new
    methods.each do |name|
      uncached_name = "#{name}_uncached".to_sym
      (class << self; self; end).class_eval do
        alias_method uncached_name, name
        define_method(name) do |*a, &b|
          cache.cache(name) { send uncached_name, *a }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basiccache-0.0.29 lib/methodcacher.rb
basiccache-0.0.27 lib/methodcacher.rb
basiccache-0.0.25 lib/methodcacher.rb