cache_helper 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ module CacheHelper
4
4
  base.extend(ClassMethods)
5
5
  end
6
6
 
7
- # Overrides ActiveRecord::Base#cache_key to return unique keys for new
7
+ # Overrides ActiveRecord::Base#cache_key to return unique cache keys for new
8
8
  # records.
9
9
  def cache_key_with_new_record_id
10
10
  key = cache_key_without_new_record_id
@@ -28,13 +28,56 @@ module CacheHelper
28
28
  key
29
29
  end
30
30
 
31
+ # Returns a cache key associated with the record for the specified method,
32
+ # associations, and options.
33
+ def method_cache_key_using_associations(method, *remaining_args)
34
+ options = remaining_args.extract_options!
35
+ associations = remaining_args
36
+ associations.each do |association|
37
+ options["#{association.to_s}_updated_time".to_sym] = association_updated_time(association)
38
+ end
39
+ method_cache_key(method, options)
40
+ end
41
+
42
+ # Returns the latest updated time of the record or records in the specified
43
+ # association. This can be used as a method_cache_key option if the method's
44
+ # return value depends on the associated records.
45
+ def association_updated_time(association)
46
+ self.class.reflect_on_all_associations.each do |assoc_reflection|
47
+ if (assoc_reflection.name == association)
48
+ case assoc_reflection.macro
49
+ when :has_one, :belongs_to
50
+ return self.__send__(association).updated_at
51
+ when :has_many, :has_and_belongs_to_many
52
+ return self.__send__(association).maximum(:updated_at)
53
+ end
54
+ end
55
+ end
56
+ raise ArgumentError.new("association #{association.inspect} not found in #{self.class.name}")
57
+ end
58
+
31
59
  module ClassMethods
32
60
  private
33
- # Wraps an instance method with basic caching functionality.
61
+ # Adds basic caching functionality to an instance method.
34
62
  def cache_method(method)
63
+ cache_method_using_cache_key_method(method, :method_cache_key, method)
64
+ end
65
+
66
+ # Adds basic caching functionality to an instance method whose return value
67
+ # depends on associated records.
68
+ def cache_method_using_associations(method, *associations)
69
+ associations.each do |association|
70
+ raise ArgumentError.new("association #{association.inspect} not found in #{self.name}") unless self.reflect_on_all_associations.any? { |assoc_reflection| assoc_reflection.name == association }
71
+ end
72
+ cache_method_using_cache_key_method(method, :method_cache_key_using_associations, method, *associations)
73
+ end
74
+
75
+ # Adds basic caching functionality to an instance method using the
76
+ # specified cache key method and arguments.
77
+ def cache_method_using_cache_key_method(method, cache_key_method, *cache_key_method_args)
35
78
  method_with_caching, method_without_caching = method_chain_aliases(method, :caching)
36
79
  define_method(method_with_caching.to_sym) {
37
- Rails.cache.fetch(method_cache_key(method)) do
80
+ Rails.cache.fetch(self.__send__(cache_key_method, *cache_key_method_args)) do
38
81
  self.__send__(method_without_caching.to_sym)
39
82
  end
40
83
  }
@@ -1,3 +1,3 @@
1
1
  module CacheHelper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_helper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Clyde Law
@@ -15,7 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-20 00:00:00 Z
18
+ date: 2011-04-20 00:00:00 -07:00
19
+ default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: activerecord
@@ -64,6 +65,7 @@ files:
64
65
  - cache_helper.gemspec
65
66
  - lib/cache_helper.rb
66
67
  - lib/cache_helper/version.rb
68
+ has_rdoc: true
67
69
  homepage: http://github.com/FutureAdvisor/cache_helper
68
70
  licenses:
69
71
  - MIT
@@ -93,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
95
  requirements: []
94
96
 
95
97
  rubyforge_project: cache_helper
96
- rubygems_version: 1.7.2
98
+ rubygems_version: 1.5.2
97
99
  signing_key:
98
100
  specification_version: 3
99
101
  summary: Adds methods to more easily work with Rails caching.