Module: ActsAsCached::InstanceMethods

Defined in:
lib/acts_as_cached/cache_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



190
191
192
193
# File 'lib/acts_as_cached/cache_methods.rb', line 190

def self.included(base)
  base.send :delegate, :cache_config,  :to => 'self.class'
  base.send :delegate, :cache_options, :to => 'self.class'
end

Instance Method Details

#cache_id(key = nil) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/acts_as_cached/cache_methods.rb', line 216

def cache_id(key = nil)
  cid = case
        when new_record?
          "new"
        when timestamp = self[:updated_at]
          timestamp = timestamp.utc.to_s(:number)
          "#{id}-#{timestamp}"
        else
          id.to_s
        end
  key.nil? ? cid : "#{cid}/#{key}"
end

#cached?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/acts_as_cached/cache_methods.rb', line 212

def cached?(key = nil)
  self.class.cached? cache_id(key)
end

#caches(method, options = {}) ⇒ Object Also known as: cached



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/acts_as_cached/cache_methods.rb', line 229

def caches(method, options = {})
  key = "#{self.cache_key}/#{method}"
  if options.keys.include?(:with)
    with = options.delete(:with)
    self.class.get_cache("#{key}/#{with}", options) { send(method, with) }
  elsif withs = options.delete(:withs)
    self.class.get_cache("#{key}/#{withs}", options) { send(method, *withs) }
  else
    self.class.get_cache(key, options) { send(method) }
  end
end

#expire_cache(key = nil) ⇒ Object Also known as: clear_cache



207
208
209
# File 'lib/acts_as_cached/cache_methods.rb', line 207

def expire_cache(key = nil)
  self.class.expire_cache(cache_id(key))
end

#expire_cache_with_associations(*associations_to_sweep) ⇒ Object

Lourens Naud



251
252
253
254
255
256
# File 'lib/acts_as_cached/cache_methods.rb', line 251

def expire_cache_with_associations(*associations_to_sweep)
  (Array(cache_options[:include]) + associations_to_sweep).flatten.uniq.compact.each do |assoc|
    Array(send(assoc)).compact.each { |item| item.expire_cache if item.respond_to?(:expire_cache) }
  end
  expire_cache
end

#get_cache(key = nil, options = {}, &block) ⇒ Object



195
196
197
# File 'lib/acts_as_cached/cache_methods.rb', line 195

def get_cache(key = nil, options = {}, &block)
  self.class.get_cache(cache_id(key), options, &block)
end

#reset_cache(key = nil) ⇒ Object



203
204
205
# File 'lib/acts_as_cached/cache_methods.rb', line 203

def reset_cache(key = nil)
  self.class.reset_cache(cache_id(key))
end

#set_cache(options = nil) ⇒ Object



199
200
201
# File 'lib/acts_as_cached/cache_methods.rb', line 199

def set_cache(options = nil)
  self.class.set_cache(cache_id, self, options)
end

#set_cache_with_associationsObject

Ryan King



243
244
245
246
247
248
# File 'lib/acts_as_cached/cache_methods.rb', line 243

def set_cache_with_associations
  Array(cache_options[:include]).each do |assoc|
    send(assoc).reload
  end if cache_options[:include]
  set_cache
end