pack_rat 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/pack_rat/active_record_extension.rb +12 -0
- data/lib/pack_rat/cache_helper/cacher.rb +23 -0
- data/lib/pack_rat/cache_helper.rb +41 -0
- data/lib/pack_rat.rb +3 -73
- data/pack_rat.gemspec +4 -1
- metadata +5 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module PackRat
|
2
|
+
module CacheHelper
|
3
|
+
module Cacher
|
4
|
+
def cache(key='', options={}, &block)
|
5
|
+
unless options[:overwrite_key]
|
6
|
+
calling_method = caller[0][/`([^']*)'/, 1]
|
7
|
+
key << calling_method << '/'
|
8
|
+
key << self.cache_key << '/'
|
9
|
+
if self.is_a? Class
|
10
|
+
key << self.file_digest
|
11
|
+
else
|
12
|
+
key << self.class.file_digest
|
13
|
+
end
|
14
|
+
end
|
15
|
+
puts key if options[:debug]
|
16
|
+
filtered_options = options.except(:overwrite_key, :debug)
|
17
|
+
Rails.cache.fetch key, filtered_options do
|
18
|
+
block.call
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'pack_rat/cache_helper/cacher'
|
2
|
+
module PackRat
|
3
|
+
module CacheHelper
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
extend Cacher
|
8
|
+
include Cacher
|
9
|
+
cattr_accessor :updated_attribute_name
|
10
|
+
self.updated_attribute_name ||= :updated_at
|
11
|
+
cattr_accessor :file_location
|
12
|
+
self.file_location = file_location_guesser
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
# Create cache_key for class, use most recently updated record
|
17
|
+
unless self.respond_to? :cache_key
|
18
|
+
define_method :cache_key do
|
19
|
+
key = order("#{self.updated_attribute_name} DESC").first.cache_key if self.superclass.to_s == "ActiveRecord::Base"
|
20
|
+
key << "/#{self.to_s}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def file_digest
|
25
|
+
if self.file_location
|
26
|
+
begin
|
27
|
+
file = File.read(self.file_location)
|
28
|
+
Digest::MD5.hexdigest(file)
|
29
|
+
rescue
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def file_location_guesser
|
36
|
+
"#{Rails.root}/app/models/#{self.to_s.split('::').join('/').underscore.downcase}.rb" if defined? Rails
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/pack_rat.rb
CHANGED
@@ -1,76 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
included do
|
6
|
-
extend Cacher
|
7
|
-
include Cacher
|
8
|
-
cattr_accessor :updated_attribute_name
|
9
|
-
self.updated_attribute_name ||= :updated_at
|
10
|
-
cattr_accessor :file_location
|
11
|
-
self.file_location = file_location_guesser
|
12
|
-
end
|
13
|
-
|
14
|
-
module Cacher
|
15
|
-
def cache(key='', options={}, &block)
|
16
|
-
unless options[:overwrite_key]
|
17
|
-
calling_method = caller[0][/`([^']*)'/, 1]
|
18
|
-
key << calling_method << '/'
|
19
|
-
key << self.cache_key << '/'
|
20
|
-
if self.is_a? Class
|
21
|
-
key << self.file_digest
|
22
|
-
else
|
23
|
-
key << self.class.file_digest
|
24
|
-
end
|
25
|
-
end
|
26
|
-
puts key if options[:debug]
|
27
|
-
filtered_options = options.except(:overwrite_key, :debug)
|
28
|
-
Rails.cache.fetch key, filtered_options do
|
29
|
-
block.call
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
module ClassMethods
|
35
|
-
# Create cache_key for class, use most recently updated record
|
36
|
-
unless self.respond_to? :cache_key
|
37
|
-
define_method :cache_key do
|
38
|
-
key = order("#{self.updated_attribute_name} DESC").first.cache_key if self.superclass.to_s == "ActiveRecord::Base"
|
39
|
-
key << "/#{self.to_s}"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def file_digest
|
44
|
-
if self.file_location
|
45
|
-
begin
|
46
|
-
file = File.read(self.file_location)
|
47
|
-
Digest::MD5.hexdigest(file)
|
48
|
-
rescue
|
49
|
-
nil
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def file_location_guesser
|
55
|
-
"#{Rails.root}/app/models/#{self.to_s.split('::').join('/').underscore.downcase}.rb" if defined? Rails
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
module ActiveRecordExtension
|
62
|
-
extend ActiveSupport::Concern
|
63
|
-
|
64
|
-
module ClassMethods
|
65
|
-
def inherited(child_class)
|
66
|
-
child_class.send(:include, PackRat::CacheHelper)
|
67
|
-
super
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
|
1
|
+
require 'active_support'
|
2
|
+
require 'pack_rat/active_record_extension'
|
3
|
+
require 'pack_rat/cache_helper'
|
74
4
|
if defined? ActiveRecord::Base
|
75
5
|
ActiveRecord::Base.send(:include, PackRat::ActiveRecordExtension)
|
76
6
|
end
|
data/pack_rat.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "pack_rat"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brian Goff"]
|
@@ -25,6 +25,9 @@ Gem::Specification.new do |s|
|
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
27
|
"lib/pack_rat.rb",
|
28
|
+
"lib/pack_rat/active_record_extension.rb",
|
29
|
+
"lib/pack_rat/cache_helper.rb",
|
30
|
+
"lib/pack_rat/cache_helper/cacher.rb",
|
28
31
|
"pack_rat.gemspec",
|
29
32
|
"spec/spec_helper.rb"
|
30
33
|
]
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: pack_rat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 1.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brian Goff
|
@@ -108,6 +108,9 @@ files:
|
|
108
108
|
- Rakefile
|
109
109
|
- VERSION
|
110
110
|
- lib/pack_rat.rb
|
111
|
+
- lib/pack_rat/active_record_extension.rb
|
112
|
+
- lib/pack_rat/cache_helper.rb
|
113
|
+
- lib/pack_rat/cache_helper/cacher.rb
|
111
114
|
- pack_rat.gemspec
|
112
115
|
- spec/spec_helper.rb
|
113
116
|
homepage: http://github.com/cpuguy83/pack_rat
|
@@ -124,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
127
|
version: '0'
|
125
128
|
segments:
|
126
129
|
- 0
|
127
|
-
hash:
|
130
|
+
hash: -2074664687036877359
|
128
131
|
none: false
|
129
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
133
|
requirements:
|