tins 0.5.6 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/tins/memoize.rb +15 -30
- data/lib/tins/version.rb +1 -1
- data/tests/memoize_test.rb +10 -4
- data/tins.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/tins/memoize.rb
CHANGED
@@ -1,34 +1,29 @@
|
|
1
1
|
module Tins
|
2
2
|
module Memoize
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@__memoize_cache__ ||= {}
|
9
|
-
end
|
3
|
+
module CacheMethods
|
4
|
+
# Return the cache object.
|
5
|
+
def __memoize_cache__
|
6
|
+
@__memoize_cache__ ||= {}
|
7
|
+
end
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
__memoize_cache__.delete(id)
|
16
|
-
end
|
17
|
-
end
|
9
|
+
# Clear cached values for all methods/functions.
|
10
|
+
def memoize_cache_clear
|
11
|
+
__memoize_cache__.clear
|
12
|
+
self
|
18
13
|
end
|
14
|
+
end
|
19
15
|
|
16
|
+
class ::Module
|
20
17
|
# Automatically memoize calls of the the methods +method_ids+. The
|
21
18
|
# memoized results do NOT ONLY depend on the arguments, but ALSO on the
|
22
19
|
# object the method is called on.
|
23
20
|
def memoize_method(*method_ids)
|
21
|
+
include CacheMethods
|
24
22
|
method_ids.each do |method_id|
|
25
23
|
method_id = method_id.to_s.to_sym
|
26
24
|
orig_method = instance_method(method_id)
|
27
25
|
__send__(:define_method, method_id) do |*args|
|
28
|
-
|
29
|
-
mc = ::Module.__memoize_cache__[__id__] ||= {}
|
30
|
-
ObjectSpace.define_finalizer(self, ::Module.__memoize_cache_delete__)
|
31
|
-
end
|
26
|
+
mc = __memoize_cache__
|
32
27
|
if mc.key?(method_id) and result = mc[method_id][args]
|
33
28
|
result
|
34
29
|
else
|
@@ -40,16 +35,13 @@ module Tins
|
|
40
35
|
end
|
41
36
|
end
|
42
37
|
|
43
|
-
|
44
|
-
def __memoize_cache__
|
45
|
-
@__memoize_cache__
|
46
|
-
end
|
38
|
+
include CacheMethods
|
47
39
|
|
48
40
|
# Automatically memoize calls of the functions +function_ids+. The
|
49
41
|
# memoized result does ONLY depend on the arguments given to the
|
50
42
|
# function.
|
51
43
|
def memoize_function(*function_ids)
|
52
|
-
mc =
|
44
|
+
mc = __memoize_cache__
|
53
45
|
function_ids.each do |method_id|
|
54
46
|
method_id = method_id.to_s.to_sym
|
55
47
|
orig_method = instance_method(method_id)
|
@@ -64,13 +56,6 @@ module Tins
|
|
64
56
|
end
|
65
57
|
end
|
66
58
|
end
|
67
|
-
|
68
|
-
# Clear cached values for all methods and functions.
|
69
|
-
def memoize_cache_clear
|
70
|
-
mc = @__memoize_cache__ and mc.clear
|
71
|
-
mc = ::Module.__memoize_cache__ and mc.clear
|
72
|
-
self
|
73
|
-
end
|
74
59
|
end
|
75
60
|
end
|
76
61
|
end
|
data/lib/tins/version.rb
CHANGED
data/tests/memoize_test.rb
CHANGED
@@ -17,24 +17,30 @@ module Tins
|
|
17
17
|
memoize_function :bar
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def test_method_cache
|
21
21
|
fb1 = FooBar.new
|
22
22
|
fb2 = FooBar.new
|
23
|
+
assert_equal true, fb1.__memoize_cache__.empty?
|
24
|
+
assert_equal true, fb2.__memoize_cache__.empty?
|
23
25
|
assert_equal 1, fb1.foo(1, 2)
|
24
26
|
assert_equal 2, fb2.foo(1, 2)
|
25
27
|
assert_equal 3, fb1.foo(1, 2, 3)
|
26
28
|
assert_equal 4, fb2.foo(1, 2, 3)
|
27
29
|
assert_equal 1, fb1.foo(1, 2)
|
28
30
|
assert_equal 2, fb2.foo(1, 2)
|
29
|
-
|
31
|
+
fb1.memoize_cache_clear
|
32
|
+
fb2.memoize_cache_clear
|
33
|
+
assert_equal true, fb1.__memoize_cache__.empty?
|
34
|
+
assert_equal true, fb2.__memoize_cache__.empty?
|
30
35
|
assert_equal 5, fb1.foo(1, 2)
|
31
36
|
assert_equal 6, fb2.foo(1, 2)
|
32
37
|
assert_equal 5, fb1.foo(1, 2)
|
33
38
|
assert_equal 6, fb2.foo(1, 2)
|
34
|
-
assert_equal false,
|
39
|
+
assert_equal false, fb1.__memoize_cache__.empty?
|
40
|
+
assert_equal false, fb2.__memoize_cache__.empty?
|
35
41
|
end
|
36
42
|
|
37
|
-
def
|
43
|
+
def test_function_cache
|
38
44
|
fb1 = FooBar.new
|
39
45
|
fb2 = FooBar.new
|
40
46
|
assert_equal 1, fb1.bar(1, 2)
|
data/tins.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tins
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -296,7 +296,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
296
296
|
version: '0'
|
297
297
|
segments:
|
298
298
|
- 0
|
299
|
-
hash:
|
299
|
+
hash: 4391703042856250428
|
300
300
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
301
301
|
none: false
|
302
302
|
requirements:
|