Module: Cacheable

Extended by:
ActiveSupport::Concern
Defined in:
lib/model_cache/cacheable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/model_cache/cacheable.rb', line 17

def method_missing(method, *arguments, &block)
  if method.to_s =~ /^cached_(.*)$/
    cache $1 do
      value = send $1.to_sym
      value = value.to_a if value.is_a? ActiveRecord::Relation
      value
    end
  else
    super
  end
end

Class Method Details

.respond_to?(method_sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/model_cache/cacheable.rb', line 30

def self.respond_to?(method_sym, include_private = false)
  if method.to_s =~ /^cached_(.*)$/
    true
  else
    super
  end
end

Instance Method Details

#cache(cache_key, options = {}, &block) ⇒ Object



11
12
13
14
# File 'lib/model_cache/cacheable.rb', line 11

def cache(cache_key, options = {}, &block)
  cache_key = ActiveSupport::Cache.expand_cache_key (Array(cache_key) + [self, updated_at])
  Rails.cache.fetch cache_key, options, &block
end