tobias-cache_value 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/cache_value.gemspec +6 -4
- data/lib/cache_value/cache_machine.rb +3 -3
- data/lib/cache_value/cache_value.rb +2 -2
- data/lib/cache_value/util.rb +20 -0
- data/test/cache_machine_test.rb +2 -2
- data/test/cache_value_test.rb +1 -1
- data/test/util_test.rb +15 -0
- metadata +4 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/cache_value.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{cache_value}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.3.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Tobias Crawley"]
|
9
|
-
s.date = %q{2009-07-
|
9
|
+
s.date = %q{2009-07-14}
|
10
10
|
s.email = %q{tcrawley@gmail.com}
|
11
11
|
s.extra_rdoc_files = [
|
12
12
|
"LICENSE",
|
@@ -26,7 +26,8 @@ Gem::Specification.new do |s|
|
|
26
26
|
"lib/cache_value/util.rb",
|
27
27
|
"test/cache_machine_test.rb",
|
28
28
|
"test/cache_value_test.rb",
|
29
|
-
"test/test_helper.rb"
|
29
|
+
"test/test_helper.rb",
|
30
|
+
"test/util_test.rb"
|
30
31
|
]
|
31
32
|
s.has_rdoc = true
|
32
33
|
s.homepage = %q{http://github.com/tobias/cache_value}
|
@@ -37,7 +38,8 @@ Gem::Specification.new do |s|
|
|
37
38
|
s.test_files = [
|
38
39
|
"test/cache_machine_test.rb",
|
39
40
|
"test/cache_value_test.rb",
|
40
|
-
"test/test_helper.rb"
|
41
|
+
"test/test_helper.rb",
|
42
|
+
"test/util_test.rb"
|
41
43
|
]
|
42
44
|
|
43
45
|
if s.respond_to? :specification_version then
|
@@ -3,7 +3,6 @@ require 'active_support/core_ext/class'
|
|
3
3
|
require 'active_support/inflector'
|
4
4
|
require 'active_support/cache'
|
5
5
|
require 'cache_value/util'
|
6
|
-
require 'sha1'
|
7
6
|
|
8
7
|
module CacheValue
|
9
8
|
class CacheMachine
|
@@ -36,7 +35,7 @@ module CacheValue
|
|
36
35
|
def cache_key(object, method, arguments = nil)
|
37
36
|
raise ConfigurationException.new("object of class #{object.class.name} does not respond to :cache_key") unless object.respond_to?(:cache_key)
|
38
37
|
key = object.cache_key.gsub('/', '_') + "_#{method}"
|
39
|
-
key << '_' +
|
38
|
+
key << '_' + hex_digest(arguments) if arguments
|
40
39
|
key
|
41
40
|
end
|
42
41
|
|
@@ -51,7 +50,7 @@ module CacheValue
|
|
51
50
|
|
52
51
|
def call_and_store_value(object, method, arguments = nil)
|
53
52
|
without_method = caching_method_names(method).first
|
54
|
-
value = arguments ? object.send(without_method, arguments) : object.send(without_method)
|
53
|
+
value = arguments ? object.send(without_method, *arguments) : object.send(without_method)
|
55
54
|
cache_store.write(cache_key(object, method, arguments), [value, Time.now].to_yaml)
|
56
55
|
|
57
56
|
value
|
@@ -65,6 +64,7 @@ module CacheValue
|
|
65
64
|
cached_age > Time.now - options
|
66
65
|
end
|
67
66
|
end
|
67
|
+
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -8,8 +8,8 @@ module CacheValue
|
|
8
8
|
def cache_value(method, option)
|
9
9
|
without_method, with_method = caching_method_names(method)
|
10
10
|
class_eval do
|
11
|
-
define_method with_method do
|
12
|
-
CacheValue::CacheMachine.lookup(self, method, option)
|
11
|
+
define_method with_method do |*args|
|
12
|
+
CacheValue::CacheMachine.lookup(self, method, option, args)
|
13
13
|
end
|
14
14
|
|
15
15
|
alias_method without_method, method
|
data/lib/cache_value/util.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'sha1'
|
2
|
+
|
1
3
|
module CacheValue
|
2
4
|
module Util
|
3
5
|
def caching_method_names(method)
|
@@ -6,5 +8,23 @@ module CacheValue
|
|
6
8
|
["#{washed_method}_without_value_caching#{punctuation}",
|
7
9
|
"#{washed_method}_with_value_caching#{punctuation}"]
|
8
10
|
end
|
11
|
+
|
12
|
+
def hex_digest(values)
|
13
|
+
Digest::SHA1.hexdigest(stringify_value(values))
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
def stringify_value(value)
|
18
|
+
if value.respond_to?(:to_str)
|
19
|
+
value.to_str
|
20
|
+
elsif value.respond_to?(:cache_key)
|
21
|
+
value.cache_key.to_s
|
22
|
+
elsif value.respond_to?(:collect)
|
23
|
+
value.collect { |x| stringify_value(x) }.join
|
24
|
+
else
|
25
|
+
value.to_s
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
9
29
|
end
|
10
30
|
end
|
data/test/cache_machine_test.rb
CHANGED
@@ -4,7 +4,7 @@ require 'cache_value/cache_machine'
|
|
4
4
|
|
5
5
|
|
6
6
|
class CacheMachineTest < Test::Unit::TestCase
|
7
|
-
|
7
|
+
|
8
8
|
def setup
|
9
9
|
@cm = CacheValue::CacheMachine
|
10
10
|
@obj = mock
|
@@ -68,7 +68,7 @@ class CacheMachineTest < Test::Unit::TestCase
|
|
68
68
|
|
69
69
|
should 'should include the hash in the key' do
|
70
70
|
now = Time.now
|
71
|
-
assert_match /_method_#{
|
71
|
+
assert_match /_method_#{@cm.send(:hex_digest, [1, 2, now])}$/, @cm.cache_key(@obj, :method, [1, 2, now])
|
72
72
|
end
|
73
73
|
|
74
74
|
end
|
data/test/cache_value_test.rb
CHANGED
@@ -20,7 +20,7 @@ class CacheValueTest < Test::Unit::TestCase
|
|
20
20
|
end
|
21
21
|
|
22
22
|
should 'delegate the cache lookup to CacheMachine' do
|
23
|
-
CacheValue::CacheMachine.expects(:lookup).with(@cacher, :do_something, 'yo')
|
23
|
+
CacheValue::CacheMachine.expects(:lookup).with(@cacher, :do_something, 'yo', [])
|
24
24
|
@cacher.do_something
|
25
25
|
end
|
26
26
|
|
data/test/util_test.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tobias-cache_value
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Crawley
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-14 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- test/cache_machine_test.rb
|
38
38
|
- test/cache_value_test.rb
|
39
39
|
- test/test_helper.rb
|
40
|
+
- test/util_test.rb
|
40
41
|
has_rdoc: true
|
41
42
|
homepage: http://github.com/tobias/cache_value
|
42
43
|
post_install_message:
|
@@ -67,3 +68,4 @@ test_files:
|
|
67
68
|
- test/cache_machine_test.rb
|
68
69
|
- test/cache_value_test.rb
|
69
70
|
- test/test_helper.rb
|
71
|
+
- test/util_test.rb
|