Module: SimpleCache::SimplyCached

Included in:
Module
Defined in:
lib/simple_cache.rb

Overview

The SimplyCached module contains a method to easily implement memoized-like caching on top of SimpleCache

Instance Method Summary collapse

Instance Method Details

#simply_cached(method, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/simple_cache.rb', line 119

def simply_cached(method, options = {})
  ttl = options[:ttl]

  uncached_method = "#{method}__uncached".to_sym

  alias_method uncached_method, method

  define_method(method) do |*args|
    cache_key = args.empty? ? self : [ self ] + args
    current_ttl = if ttl.respond_to?(:call)
      ttl.send(:call, *args)
    else
      ttl
    end

    SimpleCache.cached(cache_key, current_ttl) do
      self.send uncached_method, *args
    end 
  end
end