covercache 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/covercache.gemspec +1 -3
- data/lib/covercache/version.rb +1 -1
- data/lib/covercache.rb +44 -53
- data/spec/covercache.sqlite3 +0 -0
- data/spec/libs/covercache_spec.rb +9 -1
- data/spec/spec_helper.rb +2 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd4db05a4c6363e98a948daeb16b2968cded4169
|
4
|
+
data.tar.gz: c3fc3a7e634958920502b7ff6971e919a0ddbb84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a2a4d9e1289249fae1b850db1dc2d09b30520a923bb45da804a2db93f1c99b434b88cea512dbb4f27c1989cf84c5d1199544fc2dc036d6de92b36a08131d810
|
7
|
+
data.tar.gz: b08f824c1445ca69f4d1c8b6a818dd3537f86c5ad9ef9884b898498eed1aba22f40903e601ac142e708f6c34c9362a0ed3d05cdea402a95f275050f4af56eed0
|
data/covercache.gemspec
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
2
|
lib = File.expand_path('../lib', __FILE__)
|
4
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
4
|
require 'covercache/version'
|
@@ -21,12 +20,11 @@ Gem::Specification.new do |spec|
|
|
21
20
|
|
22
21
|
spec.add_dependency "activesupport", '>= 3.2.0', '< 4'
|
23
22
|
spec.add_dependency "activerecord", '>= 3.2.0', '< 4'
|
24
|
-
|
23
|
+
|
25
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
26
25
|
spec.add_development_dependency "rails", '>= 3.2.0', '< 4'
|
27
26
|
spec.add_development_dependency "rdoc"
|
28
27
|
spec.add_development_dependency "rspec"
|
29
28
|
spec.add_development_dependency "rake"
|
30
29
|
spec.add_development_dependency "sqlite3"
|
31
|
-
|
32
30
|
end
|
data/lib/covercache/version.rb
CHANGED
data/lib/covercache.rb
CHANGED
@@ -52,30 +52,27 @@ require 'active_record'
|
|
52
52
|
# Comments.cached_for_post_ids post_ids, cache_key: post_ids.hash
|
53
53
|
#
|
54
54
|
module Covercache
|
55
|
-
|
55
|
+
# General helper method (ex <tt>cache</tt> helper in PackRat)
|
56
56
|
def self.logger
|
57
57
|
@logger ||= rails_logger || default_logger
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
def self.rails_logger
|
61
61
|
(defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) ||
|
62
62
|
(defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:debug) && RAILS_DEFAULT_LOGGER)
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
def self.default_logger
|
66
66
|
require 'logger'
|
67
67
|
l = Logger.new(STDOUT)
|
68
68
|
l.level = Logger::DEBUG
|
69
69
|
l
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
def self.logger=(logger)
|
73
73
|
@logger = logger
|
74
74
|
end
|
75
75
|
|
76
|
-
|
77
|
-
|
78
|
-
# General helper method (ex <tt>cache</tt> helper in PackRat)
|
79
76
|
module Base
|
80
77
|
# Arguments:
|
81
78
|
#
|
@@ -103,36 +100,34 @@ module Covercache
|
|
103
100
|
#
|
104
101
|
private
|
105
102
|
def covercache(*keys, &block)
|
106
|
-
|
107
|
-
|
108
|
-
cover_opts = options.extract! :debug, :without_auto_key
|
109
|
-
|
110
|
-
# if :no_auto_cache_keys was set, we skip creating our own key
|
111
|
-
keys.prepend get_auto_cache_key(klass.name, caller) unless cover_opts[:without_auto_key]
|
103
|
+
options = keys.extract_options!
|
104
|
+
debug, without_auto_key = options.slice! :debug, :without_auto_key
|
112
105
|
|
113
|
-
keys.
|
106
|
+
keys.prepend get_auto_cache_key(caller) unless !!without_auto_key
|
107
|
+
keys.flatten!.compact!
|
114
108
|
|
115
|
-
if !!
|
116
|
-
Covercache.logger.debug keys.inspect
|
117
|
-
end
|
118
|
-
# puts caller.inspect if !!cover_opts[:debug],
|
109
|
+
Covercache.logger.debug %([covercache] #{get_class_name} class generate cache key: #{keys.inspect}) if !!debug
|
119
110
|
|
120
111
|
Rails.cache.fetch keys, options do
|
121
|
-
|
122
|
-
Covercache.logger.debug(klass.covercache_keys.inspect) if !!cover_opts[:debug]
|
112
|
+
push_covercache_key keys.join('/')
|
123
113
|
block.call
|
124
114
|
end
|
125
115
|
end
|
126
116
|
|
127
|
-
def
|
117
|
+
def push_covercache_key(key)
|
118
|
+
self.covercache_keys << key
|
119
|
+
self.covercache_keys.uniq!
|
120
|
+
end
|
121
|
+
|
122
|
+
def get_auto_cache_key(_caller)
|
128
123
|
caller_method = _caller.map {|c| c[/`([^']*)'/, 1] }.detect {|m| !m.start_with?('block') }
|
129
|
-
keys = [
|
130
|
-
keys << cache_key if
|
124
|
+
keys = [get_class_name, covercache_model_digest, caller_method]
|
125
|
+
keys << cache_key if respond_to?(:cache_key)
|
131
126
|
keys
|
132
127
|
end
|
133
128
|
|
134
|
-
def
|
135
|
-
self.
|
129
|
+
def get_class_name
|
130
|
+
self.instance_of?(Class) ? self.name : self.class.name
|
136
131
|
end
|
137
132
|
|
138
133
|
def extract_cache_key(*args)
|
@@ -140,8 +135,6 @@ module Covercache
|
|
140
135
|
end
|
141
136
|
end
|
142
137
|
|
143
|
-
|
144
|
-
|
145
138
|
# == Defining Helper
|
146
139
|
#
|
147
140
|
module DefiningHelper
|
@@ -161,14 +154,14 @@ module Covercache
|
|
161
154
|
options = args.extract_options!
|
162
155
|
options[:is_class_method] = true
|
163
156
|
args << options
|
164
|
-
self.send :define_cached, method, *Array(args
|
157
|
+
self.send :define_cached, method, *Array.wrap(args), &block
|
165
158
|
end
|
166
159
|
|
167
160
|
private
|
168
161
|
def covercache_define_wrapper(original_method, file, line, is_class_method = false)
|
169
162
|
method = "#{'self.' if is_class_method}cached_#{ original_method }"
|
170
163
|
|
171
|
-
class_eval <<-
|
164
|
+
class_eval <<-EOT, __FILE__, __LINE__ - 2
|
172
165
|
def #{method}(*args, &block) # def cached_example(*args, &block)
|
173
166
|
options = Array(#{method}_data[:args]) + extract_cache_key(*args) # options = Array(cached_example_data[:args]) + extract_cache_key(*args)
|
174
167
|
covercache *options, #{method}_data[:opts] do # covercache *options, cached_example_data[:opts] do
|
@@ -179,8 +172,8 @@ module Covercache
|
|
179
172
|
self.send :#{original_method}, *args, &block # self.send :example, *args, &block
|
180
173
|
end # end
|
181
174
|
end # end
|
182
|
-
end #
|
183
|
-
|
175
|
+
end # env
|
176
|
+
EOT
|
184
177
|
end
|
185
178
|
|
186
179
|
def covercache_method_arguments(method, *args, &block)
|
@@ -189,7 +182,7 @@ module Covercache
|
|
189
182
|
end
|
190
183
|
|
191
184
|
def organize_cached_method_data(*args, &block)
|
192
|
-
Hash[%w{args opts block}.map { |key| [key, (args.shift || block)] }].to_options
|
185
|
+
x = Hash[%w{args opts block}.map { |key| [key, (args.shift || block)] }].to_options
|
193
186
|
end
|
194
187
|
end
|
195
188
|
|
@@ -199,15 +192,16 @@ module Covercache
|
|
199
192
|
module ModelConcern
|
200
193
|
extend ActiveSupport::Concern
|
201
194
|
|
202
|
-
included do
|
203
|
-
|
204
|
-
|
205
|
-
self.send(:"covercache_#{key}=", value) if value.present?
|
195
|
+
included do
|
196
|
+
cattr_accessor :covercache_keys do
|
197
|
+
[]
|
206
198
|
end
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
199
|
+
|
200
|
+
cattr_accessor :covercache_model_source do
|
201
|
+
@covercache_caller_source
|
202
|
+
end
|
203
|
+
|
204
|
+
cattr_accessor :covercache_model_digest
|
211
205
|
generate_model_digest!
|
212
206
|
|
213
207
|
after_commit :covercache_flush_cache!
|
@@ -219,7 +213,7 @@ module Covercache
|
|
219
213
|
# Support class methods
|
220
214
|
module ClassMethods
|
221
215
|
def generate_model_digest
|
222
|
-
return unless covercache_model_source?
|
216
|
+
return unless covercache_model_source.present?
|
223
217
|
file = File.read self.covercache_model_source
|
224
218
|
Digest::MD5.hexdigest(file)
|
225
219
|
rescue
|
@@ -246,20 +240,17 @@ module Covercache
|
|
246
240
|
end
|
247
241
|
end
|
248
242
|
|
249
|
-
|
250
|
-
#
|
251
|
-
|
252
|
-
|
253
|
-
def covers_with_cache
|
254
|
-
caller_source = caller.first[/[^:]+/]
|
243
|
+
# module CoversWithCache
|
244
|
+
# add Covercache supporting to model
|
245
|
+
def covers_with_cache
|
246
|
+
caller_source = caller.first[/[^:]+/]
|
255
247
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
end
|
248
|
+
class_eval do
|
249
|
+
@covercache_caller_source = caller_source
|
250
|
+
include Covercache::ModelConcern
|
251
|
+
extend Covercache::DefiningHelper
|
261
252
|
end
|
262
253
|
end
|
263
254
|
end
|
264
255
|
|
265
|
-
ActiveRecord::Base.extend Covercache
|
256
|
+
ActiveRecord::Base.extend Covercache
|
data/spec/covercache.sqlite3
CHANGED
Binary file
|
@@ -16,7 +16,7 @@ describe "covercache" do
|
|
16
16
|
Post.covercache_model_digest.should be_an(String)
|
17
17
|
end
|
18
18
|
|
19
|
-
it
|
19
|
+
it "should respounds to instance method defined by define_cached" do
|
20
20
|
comment = Comment.last
|
21
21
|
comment.cached_post.should == comment.post
|
22
22
|
end
|
@@ -52,4 +52,12 @@ describe "covercache" do
|
|
52
52
|
Comment.covercache_keys.should be_an(Array)
|
53
53
|
Comment.covercache_keys.count.should > 0
|
54
54
|
end
|
55
|
+
|
56
|
+
it 'it should clean storage' do
|
57
|
+
Comment.find(1).covercache_flush_cache!
|
58
|
+
comments = Comment.cached_for_post(1, cache_key: 1)
|
59
|
+
pp Comment.covercache_keys
|
60
|
+
Comment.covercache_keys.should be_an(Array)
|
61
|
+
Comment.covercache_keys.count.should == 1
|
62
|
+
end
|
55
63
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -16,14 +16,9 @@ ActiveRecord::Base.establish_connection adapter: "sqlite3",
|
|
16
16
|
database: File.expand_path("../covercache.sqlite3", __FILE__)
|
17
17
|
|
18
18
|
class Rails
|
19
|
+
RAILS_CACHE = ActiveSupport::Cache::MemoryStore.new
|
19
20
|
def self.cache
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class Rails::Cache
|
25
|
-
def self.fetch(*options, &block)
|
26
|
-
block.call
|
21
|
+
RAILS_CACHE
|
27
22
|
end
|
28
23
|
end
|
29
24
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: covercache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tõnis Simo
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|