spruz 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -3
- data/VERSION +1 -1
- data/lib/spruz/memoize.rb +17 -12
- data/lib/spruz/version.rb +1 -1
- data/tests/test_spruz_memoize.rb +52 -0
- metadata +5 -3
data/Rakefile
CHANGED
@@ -23,12 +23,13 @@ end
|
|
23
23
|
|
24
24
|
desc "Testing library"
|
25
25
|
task :test do
|
26
|
-
|
26
|
+
ENV['RUBYOPT'] = "#{ENV['RUBYOPT']} -Ilib"
|
27
|
+
sh 'testrb tests/test_*.rb'
|
27
28
|
end
|
28
29
|
|
29
30
|
desc "Testing library with coverage"
|
30
31
|
task :coverage do
|
31
|
-
sh "rcov -Ilib -x 'tests/.*\.rb' tests/
|
32
|
+
sh "rcov -Ilib -x 'tests/.*\.rb' tests/test_*.rb"
|
32
33
|
end
|
33
34
|
|
34
35
|
if defined? Gem
|
@@ -48,7 +49,7 @@ if defined? Gem
|
|
48
49
|
s.has_rdoc = true
|
49
50
|
s.extra_rdoc_files << 'README'
|
50
51
|
s.rdoc_options << '--title' << 'Spruz' << '--main' << 'README'
|
51
|
-
s.test_files
|
52
|
+
s.test_files.concat Dir['tests/test_*.rb']
|
52
53
|
|
53
54
|
s.author = "Florian Frank"
|
54
55
|
s.email = "flori@ping.de"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/spruz/memoize.rb
CHANGED
@@ -22,19 +22,18 @@ module Spruz
|
|
22
22
|
# object the method is called on.
|
23
23
|
def memoize_method(*method_ids)
|
24
24
|
method_ids.each do |method_id|
|
25
|
+
method_id = method_id.to_s.to_sym
|
25
26
|
orig_method = instance_method(method_id)
|
26
27
|
__send__(:define_method, method_id) do |*args|
|
27
28
|
unless mc = ::Module.__memoize_cache__[__id__]
|
28
29
|
mc = ::Module.__memoize_cache__[__id__] ||= {}
|
29
30
|
ObjectSpace.define_finalizer(self, ::Module.__memoize_cache_delete__)
|
30
31
|
end
|
31
|
-
if mc.key?(args
|
32
|
-
result
|
32
|
+
if mc.key?(method_id) and result = mc[method_id][args]
|
33
|
+
result
|
33
34
|
else
|
34
|
-
|
35
|
-
|
36
|
-
warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]"
|
37
|
-
end
|
35
|
+
(mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
|
36
|
+
$DEBUG and warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]"
|
38
37
|
end
|
39
38
|
result
|
40
39
|
end
|
@@ -52,20 +51,26 @@ module Spruz
|
|
52
51
|
def memoize_function(*function_ids)
|
53
52
|
mc = @__memoize_cache__ ||= {}
|
54
53
|
function_ids.each do |method_id|
|
54
|
+
method_id = method_id.to_s.to_sym
|
55
55
|
orig_method = instance_method(method_id)
|
56
56
|
__send__(:define_method, method_id) do |*args|
|
57
|
-
if mc.key?(args
|
58
|
-
result
|
57
|
+
if mc.key?(method_id) and result = mc[method_id][args]
|
58
|
+
result
|
59
59
|
else
|
60
|
-
|
61
|
-
|
62
|
-
warn "#{self.class} cached function #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect}"
|
63
|
-
end
|
60
|
+
(mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
|
61
|
+
$DEBUG and warn "#{self.class} cached function #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect}"
|
64
62
|
end
|
65
63
|
result
|
66
64
|
end
|
67
65
|
end
|
68
66
|
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
|
69
74
|
end
|
70
75
|
end
|
71
76
|
end
|
data/lib/spruz/version.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'spruz/memoize'
|
5
|
+
|
6
|
+
module Spruz
|
7
|
+
class TestSpruzMemoize < Test::Unit::TestCase
|
8
|
+
class FooBar
|
9
|
+
def foo(*a)
|
10
|
+
@@foo ||= 0
|
11
|
+
@@foo += 1
|
12
|
+
end
|
13
|
+
memoize_method :foo
|
14
|
+
|
15
|
+
def bar(*a)
|
16
|
+
@@bar ||= 0
|
17
|
+
@@bar += 1
|
18
|
+
end
|
19
|
+
memoize_function :bar
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_foo
|
23
|
+
fb1 = FooBar.new
|
24
|
+
fb2 = FooBar.new
|
25
|
+
assert_equal 1, fb1.foo(1, 2)
|
26
|
+
assert_equal 2, fb2.foo(1, 2)
|
27
|
+
assert_equal 3, fb1.foo(1, 2, 3)
|
28
|
+
assert_equal 4, fb2.foo(1, 2, 3)
|
29
|
+
assert_equal 1, fb1.foo(1, 2)
|
30
|
+
assert_equal 2, fb2.foo(1, 2)
|
31
|
+
FooBar.memoize_cache_clear
|
32
|
+
assert_equal 5, fb1.foo(1, 2)
|
33
|
+
assert_equal 6, fb2.foo(1, 2)
|
34
|
+
assert_equal 5, fb1.foo(1, 2)
|
35
|
+
assert_equal 6, fb2.foo(1, 2)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_bar
|
39
|
+
fb1 = FooBar.new
|
40
|
+
fb2 = FooBar.new
|
41
|
+
assert_equal 1, fb1.bar(1, 2)
|
42
|
+
assert_equal 1, fb2.bar(1, 2)
|
43
|
+
assert_equal 2, fb1.bar(1, 2, 3)
|
44
|
+
assert_equal 2, fb2.bar(1, 2, 3)
|
45
|
+
assert_equal 1, fb1.bar(1, 2)
|
46
|
+
assert_equal 1, fb2.bar(1, 2)
|
47
|
+
FooBar.memoize_cache_clear
|
48
|
+
assert_equal 3, fb1.bar(1, 2)
|
49
|
+
assert_equal 3, fb2.bar(1, 2)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Florian Frank
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-11-03 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- lib/spruz/xt.rb
|
73
73
|
- lib/spruz.rb
|
74
74
|
- tests/test_spruz.rb
|
75
|
+
- tests/test_spruz_memoize.rb
|
75
76
|
- install.rb
|
76
77
|
- LICENSE
|
77
78
|
has_rdoc: true
|
@@ -109,3 +110,4 @@ specification_version: 3
|
|
109
110
|
summary: Useful stuff.
|
110
111
|
test_files:
|
111
112
|
- tests/test_spruz.rb
|
113
|
+
- tests/test_spruz_memoize.rb
|