simple_cacheable 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module Cacheable
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
data/lib/cacheable.rb CHANGED
@@ -3,10 +3,13 @@ module Cacheable
3
3
  base.class_eval do
4
4
  class <<self
5
5
  def model_cache(&block)
6
+ class_attribute :cache_key, :cached_indices, :cached_methods
6
7
  instance_exec &block
7
8
  end
8
9
 
9
10
  def with_key
11
+ self.cache_key = true
12
+
10
13
  class_eval <<-EOF
11
14
  after_update :expire_key_cache
12
15
 
@@ -19,12 +22,12 @@ module Cacheable
19
22
  end
20
23
 
21
24
  def with_attribute(*attributes)
25
+ self.cached_indices = attributes.inject({}) { |indices, attribute| indices[attribute] = {} }
26
+
22
27
  class_eval <<-EOF
23
28
  after_update :expire_attribute_cache
24
29
  EOF
25
30
 
26
- class_attribute :cached_indices
27
- self.cached_indices = attributes.inject({}) { |indices, attribute| indices[attribute] = {} }
28
31
  attributes.each do |attribute|
29
32
  class_eval <<-EOF
30
33
  def self.find_cached_by_#{attribute}(value)
@@ -39,12 +42,12 @@ module Cacheable
39
42
  end
40
43
 
41
44
  def with_method(*methods)
45
+ self.cached_methods = methods
46
+
42
47
  class_eval <<-EOF
43
48
  after_update :expire_method_cache
44
49
  EOF
45
50
 
46
- class_attribute :cached_methods
47
- self.cached_methods = methods
48
51
  methods.each do |meth|
49
52
  class_eval <<-EOF
50
53
  def cached_#{meth}
@@ -104,9 +107,9 @@ module Cacheable
104
107
  end
105
108
 
106
109
  def expire_model_cache
107
- expire_key_cache
108
- expire_attribute_cache
109
- expire_method_cache
110
+ expire_key_cache if self.class.cache_key
111
+ expire_attribute_cache if self.class.cached_indices.present?
112
+ expire_method_cache if self.class.cached_methods.present?
110
113
  end
111
114
 
112
115
  def expire_key_cache
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.1
5
+ version: 1.1.2
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-07 00:00:00 +08:00
13
+ date: 2011-10-09 00:00:00 +08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency