public
Fork of defunkt/cache_fu
Description: Everyone's favorite memcached plugin for ActiveRecord.
Homepage: http://errtheblog.com
Clone URL: git://github.com/technoweenie/cache_fu.git
cache_fu / defaults / extensions.rb.default
100644 41 lines (39 sloc) 1.179 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
##
# Copy this file to vendor/plugins/acts_as_cached/extensions.rb if you
# wish to extend acts_as_cached with your own instance or class methods.
#
# You can, of course, do this directly in your cached classes,
# but keeping your custom methods here allows you to define
# methods for all cached objects DRYly.
module ActsAsCached
  module Extensions
    module ClassMethods
      ##
      # All acts_as_cached classes will be extended with
      # this method.
      #
      # >> Story.multi_get_cache(13, 353, 1231, 505)
      # => [<Story:13>, <Story:353>, ...]
      def multi_get_cache(*ids)
        ids.flatten.map { |id| get_cache(id) }
      end
    end
 
    module InstanceMethods
      ##
      # All instances of a acts_as_cached class will be
      # extended with this method.
      #
      # => story = Story.get_cache(1)
      # => <Story:1>
      # >> story.reset_included_caches
      # => true
      def reset_included_caches
        return false unless associations = cache_config[:include]
        associations.each do |association|
          Array(send(association)).each { |item| item.reset_cache }
        end
        true
      end
    end
  end
end