Module: Cacheable::ClassMethods

Defined in:
lib/model_cache/cacheable.rb

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



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/model_cache/cacheable.rb', line 62

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)


75
76
77
78
79
80
81
# File 'lib/model_cache/cacheable.rb', line 75

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



56
57
58
59
# File 'lib/model_cache/cacheable.rb', line 56

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

#updated_at(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/model_cache/cacheable.rb', line 41

def updated_at(options = {})
  value = Rails.cache.fetch "#{name}_updated_at"
  if value.blank? || options[:force]
    value = maximum :updated_at
    Rails.cache.write "#{name}_updated_at", value
  end
  value
end

#updated_at=(value) ⇒ Object



51
52
53
# File 'lib/model_cache/cacheable.rb', line 51

def updated_at=(value)
  Rails.cache.write "#{name}_updated_at", value
end