Module: Cachethod::ClassMethods
- Defined in:
- lib/cachethod.rb
Instance Method Summary collapse
- #cache_method(names, *args) ⇒ Object (also: #cache_methods, #cachethod, #cachethods)
- #cache_method_create(name, *args) ⇒ Object
- #method_added(name) ⇒ Object
Instance Method Details
#cache_method(names, *args) ⇒ Object Also known as: cache_methods, cachethod, cachethods
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cachethod.rb', line 7 def cache_method names, *args @methods_to_cache ||= {} [*names].each do |name| if instance_methods.include?(name.to_sym) cache_method_create(name, *args) else @methods_to_cache[name.to_sym] = args end end end |
#cache_method_create(name, *args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cachethod.rb', line 30 def cache_method_create name, *args define_method "#{name}_cached" do cache_key = "cachethod.#{self.class.to_s.underscore}." cache_key += "#{hash}.#{name}." cache_key += args.hash.to_s Rails.cache.fetch(cache_key, *args) do send("#{name}!") end end alias_method "#{name}!", name alias_method name, "#{name}_cached" end |
#method_added(name) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/cachethod.rb', line 23 def method_added name super return if @methods_to_cache.nil? || @methods_to_cache[name].nil? args = @methods_to_cache.delete(name) cache_method_create(name, *args) end |