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 CHANGED
@@ -1 +1 @@
1
- 0.5.6
1
+ 0.6.0
data/lib/tins/memoize.rb CHANGED
@@ -1,34 +1,29 @@
1
1
  module Tins
2
2
  module Memoize
3
- class ::Module
4
- class << self
5
- # Returns the current memoize cache for all the stored objects and
6
- # method call results.
7
- def __memoize_cache__
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
- # Finalizer to delete the stored result for a garbage collected object.
12
- def __memoize_cache_delete__
13
- lambda do |id|
14
- $DEBUG and warn "Deleted method results for object id='#{id}'"
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
- unless mc = ::Module.__memoize_cache__[__id__]
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
- # Returns the current memoize cache for this Module.
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 = @__memoize_cache__ ||= {}
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
@@ -1,6 +1,6 @@
1
1
  module Tins
2
2
  # Tins version
3
- VERSION = '0.5.6'
3
+ VERSION = '0.6.0'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -17,24 +17,30 @@ module Tins
17
17
  memoize_function :bar
18
18
  end
19
19
 
20
- def test_foo
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
- FooBar.memoize_cache_clear
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, Module.__memoize_cache__.empty?
39
+ assert_equal false, fb1.__memoize_cache__.empty?
40
+ assert_equal false, fb2.__memoize_cache__.empty?
35
41
  end
36
42
 
37
- def test_bar
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "tins"
5
- s.version = "0.5.6"
5
+ s.version = "0.6.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Florian Frank"]
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.5.6
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: 869297561121614640
299
+ hash: 4391703042856250428
300
300
  required_rubygems_version: !ruby/object:Gem::Requirement
301
301
  none: false
302
302
  requirements: