simple_cacheable 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/cacheable.rb +12 -16
  2. data/lib/cacheable/version.rb +1 -1
  3. metadata +2 -2
data/lib/cacheable.rb CHANGED
@@ -23,14 +23,13 @@ module Cacheable
23
23
  after_update :expire_attribute_cache
24
24
  EOF
25
25
 
26
- write_inheritable_attribute :cached_indices, attributes.inject({}) { |indices, attribute| indices[attribute] = {} }
26
+ class_attribute :cached_indices
27
+ self.cached_indices = attributes.inject({}) { |indices, attribute| indices[attribute] = {} }
27
28
  attributes.each do |attribute|
28
29
  class_eval <<-EOF
29
30
  def self.find_cached_by_#{attribute}(value)
30
- indices = read_inheritable_attribute :cached_indices
31
- indices["#{attribute}"] ||= []
32
- indices["#{attribute}"] << value
33
- write_inheritable_attribute :cached_indices, indices
31
+ self.cached_indices["#{attribute}"] ||= []
32
+ self.cached_indices["#{attribute}"] << value
34
33
  Rails.cache.fetch attribute_cache_key("#{attribute}", value) do
35
34
  self.find_by_#{attribute}(value)
36
35
  end
@@ -44,7 +43,8 @@ module Cacheable
44
43
  after_update :expire_method_cache
45
44
  EOF
46
45
 
47
- write_inheritable_attribute :cached_methods, methods
46
+ class_attribute :cached_methods
47
+ self.cached_methods = methods
48
48
  methods.each do |meth|
49
49
  class_eval <<-EOF
50
50
  def cached_#{meth}
@@ -88,7 +88,7 @@ module Cacheable
88
88
  class_eval <<-EOF
89
89
  def cached_#{association_name}
90
90
  Rails.cache.fetch have_association_cache_key("#{association_name}") do
91
- #{association_name}
91
+ #{association_name}.respond_to?(:all) ? #{association_name}.all : #{association_name}
92
92
  end
93
93
  end
94
94
  EOF
@@ -114,20 +114,16 @@ module Cacheable
114
114
  end
115
115
 
116
116
  def expire_attribute_cache
117
- if indices = self.class.read_inheritable_attribute(:cached_indices)
118
- indices.each do |attribute, values|
119
- values.each do |value|
120
- Rails.cache.delete self.class.attribute_cache_key(attribute, value)
121
- end
117
+ self.class.cached_indices.each do |attribute, values|
118
+ values.each do |value|
119
+ Rails.cache.delete self.class.attribute_cache_key(attribute, value)
122
120
  end
123
121
  end
124
122
  end
125
123
 
126
124
  def expire_method_cache
127
- if meths = self.class.read_inheritable_attribute(:cached_methods)
128
- meths.each do |meth|
129
- Rails.cache.delete method_cache_key(meth)
130
- end
125
+ self.class.cached_methods.each do |meth|
126
+ Rails.cache.delete method_cache_key(meth)
131
127
  end
132
128
  end
133
129
 
@@ -1,3 +1,3 @@
1
1
  module Cacheable
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple_cacheable
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.0
5
+ version: 1.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Richard Huang
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-06 00:00:00 +08:00
13
+ date: 2011-10-07 00:00:00 +08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency