tins 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa1bf4ee9dea65d5c11f563451d6642c5fe1884c
4
- data.tar.gz: 1c7146c7571c21394db79a1d390507b3895e2e44
3
+ metadata.gz: a50aca615e8441d3218a594675339c5f9f959ac3
4
+ data.tar.gz: 7cd19f7ba00d883fa186d9fe579d9bcce1143de2
5
5
  SHA512:
6
- metadata.gz: 19a5df0f5e8e5d13c1a868a369b1aa6e5ef85f75a91e37bf815c0954aefe71f4a602889ee12b37b1c12e8dce3669730ea39bd93620205391ec6012ba9eb16d53
7
- data.tar.gz: f0d23c8f50f2638bd4d06ecd56abc6fce27036dfdc67573234f02cbe9db9fefe45c8b8ae6dfa8f3e281a35d85931b40b4d813efa31ff79b0c7077cb0d11b4283
6
+ metadata.gz: c6b1b385904afff0782b6cd98f48d75f898a34cc0535291427f1c5e6eedca01f8caf2e474eee39505162a4fb8c10ce901d67aaafa92725e600e92bbed626dd2e
7
+ data.tar.gz: 18420ef7cb78ed2aa3a2901a44702fd68472e3fa6c0db0cbb42c8a6a396c21b51f157dd7cd73aaeae158f6df0f7c7ce67617036f757fb6ea2fd9adbe0e7be4b6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.1
1
+ 1.3.2
data/lib/tins/memoize.rb CHANGED
@@ -13,6 +13,20 @@ module Tins
13
13
  __memoize_cache__.clear
14
14
  self
15
15
  end
16
+
17
+ def memoize_apply_visibility(id)
18
+ visibility = instance_eval do
19
+ case
20
+ when private_method_defined?(id)
21
+ :private
22
+ when protected_method_defined?(id)
23
+ :protected
24
+ end
25
+ end
26
+ yield
27
+ ensure
28
+ visibility and __send__(visibility, id)
29
+ end
16
30
  end
17
31
 
18
32
  class ::Module
@@ -25,17 +39,19 @@ module Tins
25
39
  include CacheMethods
26
40
  method_ids.each do |method_id|
27
41
  method_id = method_id.to_s.to_sym
28
- orig_method = instance_method(method_id)
29
- __send__(:define_method, method_id) do |*args|
30
- mc = __memoize_cache__
31
- if mc.key?(method_id) and result = mc[method_id][args]
42
+ memoize_apply_visibility method_id do
43
+ orig_method = instance_method(method_id)
44
+ __send__(:define_method, method_id) do |*args|
45
+ mc = __memoize_cache__
46
+ if mc.key?(method_id) and result = mc[method_id][args]
47
+ result
48
+ else
49
+ (mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
50
+ $DEBUG and warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]"
51
+ opts[:freeze] and result.freeze
52
+ end
32
53
  result
33
- else
34
- (mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
35
- $DEBUG and warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]"
36
- opts[:freeze] and result.freeze
37
54
  end
38
- result
39
55
  end
40
56
  end
41
57
  method_ids.size == 1 ? method_ids.first : method_ids
@@ -52,16 +68,18 @@ module Tins
52
68
  mc = __memoize_cache__
53
69
  function_ids.each do |function_id|
54
70
  function_id = function_id.to_s.to_sym
55
- orig_function = instance_method(function_id)
56
- __send__(:define_method, function_id) do |*args|
57
- if mc.key?(function_id) and result = mc[function_id][args]
71
+ memoize_apply_visibility function_id do
72
+ orig_function = instance_method(function_id)
73
+ __send__(:define_method, function_id) do |*args|
74
+ if mc.key?(function_id) and result = mc[function_id][args]
75
+ result
76
+ else
77
+ (mc[function_id] ||= {})[args] = result = orig_function.bind(self).call(*args)
78
+ opts[:freeze] and result.freeze
79
+ $DEBUG and warn "#{self.class} cached function #{function_id}(#{args.inspect unless args.empty?}) = #{result.inspect}"
80
+ end
58
81
  result
59
- else
60
- (mc[function_id] ||= {})[args] = result = orig_function.bind(self).call(*args)
61
- opts[:freeze] and result.freeze
62
- $DEBUG and warn "#{self.class} cached function #{function_id}(#{args.inspect unless args.empty?}) = #{result.inspect}"
63
82
  end
64
- result
65
83
  end
66
84
  end
67
85
  function_ids.size == 1 ? function_ids.first : function_ids
data/lib/tins/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Tins
2
2
  # Tins version
3
- VERSION = '1.3.1'
3
+ VERSION = '1.3.2'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -10,11 +10,26 @@ module Tins
10
10
  end
11
11
  memoize_method :foo
12
12
 
13
+ def footsie(*a)
14
+ @@footsie ||= 0
15
+ @@footsie += 1
16
+ end
17
+ protected :footsie
18
+ memoize_method :footsie
19
+
13
20
  def bar(*a)
14
21
  @@bar ||= 0
15
22
  @@bar += 1
16
23
  end
17
24
  memoize_function :bar
25
+
26
+ private
27
+
28
+ def baz(*a)
29
+ @@baz ||= 0
30
+ @@baz += 1
31
+ end
32
+ memoize_function :baz
18
33
  end
19
34
 
20
35
  def test_method_cache
@@ -40,6 +55,31 @@ module Tins
40
55
  assert_equal false, fb2.__memoize_cache__.empty?
41
56
  end
42
57
 
58
+ def test_method_cache_protected
59
+ fb1 = FooBar.new
60
+ fb2 = FooBar.new
61
+ assert_raises(NoMethodError) { fb1.footsie(1, 2) }
62
+ assert_raises(NoMethodError) { fb2.footsie(1, 2) }
63
+ assert_equal true, fb1.__memoize_cache__.empty?
64
+ assert_equal true, fb2.__memoize_cache__.empty?
65
+ assert_equal 1, fb1.__send__(:footsie, 1, 2)
66
+ assert_equal 2, fb2.__send__(:footsie, 1, 2)
67
+ assert_equal 3, fb1.__send__(:footsie, 1, 2, 3)
68
+ assert_equal 4, fb2.__send__(:footsie, 1, 2, 3)
69
+ assert_equal 1, fb1.__send__(:footsie, 1, 2)
70
+ assert_equal 2, fb2.__send__(:footsie, 1, 2)
71
+ fb1.memoize_cache_clear
72
+ fb2.memoize_cache_clear
73
+ assert_equal true, fb1.__memoize_cache__.empty?
74
+ assert_equal true, fb2.__memoize_cache__.empty?
75
+ assert_equal 5, fb1.__send__(:footsie, 1, 2)
76
+ assert_equal 6, fb2.__send__(:footsie, 1, 2)
77
+ assert_equal 5, fb1.__send__(:footsie, 1, 2)
78
+ assert_equal 6, fb2.__send__(:footsie, 1, 2)
79
+ assert_equal false, fb1.__memoize_cache__.empty?
80
+ assert_equal false, fb2.__memoize_cache__.empty?
81
+ end
82
+
43
83
  def test_function_cache
44
84
  fb1 = FooBar.new
45
85
  fb2 = FooBar.new
@@ -54,5 +94,22 @@ module Tins
54
94
  assert_equal 3, fb2.bar(1, 2)
55
95
  assert_equal false, FooBar.__memoize_cache__.empty?
56
96
  end
97
+
98
+ def test_function_cache_private
99
+ fb1 = FooBar.new
100
+ fb2 = FooBar.new
101
+ assert_raises(NoMethodError) { fb1.baz(1, 2) }
102
+ assert_raises(NoMethodError) { fb2.baz(1, 2) }
103
+ assert_equal 1, fb1.__send__(:baz, 1, 2)
104
+ assert_equal 1, fb2.__send__(:baz, 1, 2)
105
+ assert_equal 2, fb1.__send__(:baz, 1, 2, 3)
106
+ assert_equal 2, fb2.__send__(:baz, 1, 2, 3)
107
+ assert_equal 1, fb1.__send__(:baz, 1, 2)
108
+ assert_equal 1, fb2.__send__(:baz, 1, 2)
109
+ FooBar.memoize_cache_clear
110
+ assert_equal 3, fb1.__send__(:baz, 1, 2)
111
+ assert_equal 3, fb2.__send__(:baz, 1, 2)
112
+ assert_equal false, FooBar.__memoize_cache__.empty?
113
+ end
57
114
  end
58
115
  end
data/tins.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: tins 1.3.1 ruby lib
2
+ # stub: tins 1.3.2 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "tins"
6
- s.version = "1.3.1"
6
+ s.version = "1.3.2"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Florian Frank"]
11
- s.date = "2014-08-13"
11
+ s.date = "2014-08-19"
12
12
  s.description = "All the stuff that isn't good/big enough for a real library."
13
13
  s.email = "flori@ping.de"
14
14
  s.extra_rdoc_files = ["README.md", "lib/dslkit.rb", "lib/dslkit/polite.rb", "lib/dslkit/rude.rb", "lib/spruz.rb", "lib/tins.rb", "lib/tins/alias.rb", "lib/tins/annotate.rb", "lib/tins/ask_and_send.rb", "lib/tins/attempt.rb", "lib/tins/bijection.rb", "lib/tins/case_predicate.rb", "lib/tins/concern.rb", "lib/tins/count_by.rb", "lib/tins/date_dummy.rb", "lib/tins/date_time_dummy.rb", "lib/tins/deep_const_get.rb", "lib/tins/deep_dup.rb", "lib/tins/dslkit.rb", "lib/tins/extract_last_argument_options.rb", "lib/tins/file_binary.rb", "lib/tins/find.rb", "lib/tins/generator.rb", "lib/tins/go.rb", "lib/tins/hash_symbolize_keys_recursive.rb", "lib/tins/hash_union.rb", "lib/tins/if_predicate.rb", "lib/tins/limited.rb", "lib/tins/lines_file.rb", "lib/tins/memoize.rb", "lib/tins/method_description.rb", "lib/tins/minimize.rb", "lib/tins/module_group.rb", "lib/tins/named_set.rb", "lib/tins/null.rb", "lib/tins/once.rb", "lib/tins/p.rb", "lib/tins/partial_application.rb", "lib/tins/proc_compose.rb", "lib/tins/proc_prelude.rb", "lib/tins/range_plus.rb", "lib/tins/require_maybe.rb", "lib/tins/responding.rb", "lib/tins/rotate.rb", "lib/tins/secure_write.rb", "lib/tins/sexy_singleton.rb", "lib/tins/shuffle.rb", "lib/tins/string_byte_order_mark.rb", "lib/tins/string_camelize.rb", "lib/tins/string_underscore.rb", "lib/tins/string_version.rb", "lib/tins/subhash.rb", "lib/tins/terminal.rb", "lib/tins/thread_local.rb", "lib/tins/time_dummy.rb", "lib/tins/to.rb", "lib/tins/to_proc.rb", "lib/tins/token.rb", "lib/tins/uniq_by.rb", "lib/tins/version.rb", "lib/tins/write.rb", "lib/tins/xt.rb", "lib/tins/xt/annotate.rb", "lib/tins/xt/ask_and_send.rb", "lib/tins/xt/attempt.rb", "lib/tins/xt/blank.rb", "lib/tins/xt/case_predicate.rb", "lib/tins/xt/concern.rb", "lib/tins/xt/count_by.rb", "lib/tins/xt/date_dummy.rb", "lib/tins/xt/date_time_dummy.rb", "lib/tins/xt/deep_const_get.rb", "lib/tins/xt/deep_dup.rb", "lib/tins/xt/dslkit.rb", "lib/tins/xt/extract_last_argument_options.rb", "lib/tins/xt/file_binary.rb", "lib/tins/xt/full.rb", "lib/tins/xt/hash_symbolize_keys_recursive.rb", "lib/tins/xt/hash_union.rb", "lib/tins/xt/if_predicate.rb", "lib/tins/xt/irb.rb", "lib/tins/xt/method_description.rb", "lib/tins/xt/named.rb", "lib/tins/xt/null.rb", "lib/tins/xt/p.rb", "lib/tins/xt/partial_application.rb", "lib/tins/xt/proc_compose.rb", "lib/tins/xt/proc_prelude.rb", "lib/tins/xt/range_plus.rb", "lib/tins/xt/require_maybe.rb", "lib/tins/xt/responding.rb", "lib/tins/xt/rotate.rb", "lib/tins/xt/secure_write.rb", "lib/tins/xt/sexy_singleton.rb", "lib/tins/xt/shuffle.rb", "lib/tins/xt/string.rb", "lib/tins/xt/string_byte_order_mark.rb", "lib/tins/xt/string_camelize.rb", "lib/tins/xt/string_underscore.rb", "lib/tins/xt/string_version.rb", "lib/tins/xt/subhash.rb", "lib/tins/xt/symbol_to_proc.rb", "lib/tins/xt/time_dummy.rb", "lib/tins/xt/time_freezer.rb", "lib/tins/xt/to.rb", "lib/tins/xt/uniq_by.rb", "lib/tins/xt/write.rb"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tins
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-13 00:00:00.000000000 Z
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar