mem 0.1.4 → 0.1.5

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: 1f73463cfd8433821a0bcf5965e3723f5664108c
4
- data.tar.gz: ef54399be1bd91d4e7c6e4578cb664a7b50a83e1
3
+ metadata.gz: 3dabbde891da1e6d7fdfbc10036bdf09dfc96c84
4
+ data.tar.gz: be92cb3a9996e6a958c9b7493ae6bbca3be6e2e5
5
5
  SHA512:
6
- metadata.gz: afe455e5ff5efe99f0ebd6e80535fec3e2eff047aa1274caec7a601c10000767901554699c94dfbd96ef8560cc9b02ec63bf44574d8dc5906ec62e17226b6ba4
7
- data.tar.gz: 225b1a658713bb109863af892609ed60ddf61463b671509745fe6c3d52a3b1664e47044b197e91e97cb774ad5c92915fa4600532c31da94f783c5b234ca7b26c
6
+ metadata.gz: 06414b62be4874cabbac535aef1864a858ff6d89d445c31bbba840396d2da508e8fff08933b06b96e6fac2db65289a1a209b696eb76e03188022a6903760d6d8
7
+ data.tar.gz: f7fe1c3e822e8718483d4bbd7e23490bf95b05bbca9ec99da3e74de7484476b5d90a9176dd99fcd8cb246d86e4ee97acf4a3982d69af2dad6a9b8cb933a08bf0
@@ -1,3 +1,6 @@
1
+ ## 0.1.5
2
+ * Fix method visibility
3
+
1
4
  ## 0.1.4
2
5
  * Add support for removing memoization
3
6
 
data/lib/mem.rb CHANGED
@@ -27,6 +27,16 @@ module Mem
27
27
 
28
28
  module ClassMethods
29
29
  def memoize(method_name)
30
+ original_visibility =
31
+ case
32
+ when protected_instance_methods.include?(method_name)
33
+ :protected
34
+ when private_instance_methods.include?(method_name)
35
+ :private
36
+ else
37
+ :public
38
+ end
39
+
30
40
  define_method("#{method_name}_with_memoize") do |*args, &block|
31
41
  if has_memoized?(method_name)
32
42
  memoized(method_name)
@@ -34,15 +44,21 @@ module Mem
34
44
  memoize(method_name, send("#{method_name}_without_memoize", *args, &block))
35
45
  end
36
46
  end
47
+ send(original_visibility, "#{method_name}_with_memoize")
48
+
37
49
  alias_method "#{method_name}_without_memoize", method_name
38
50
  alias_method method_name, "#{method_name}_with_memoize"
39
51
 
40
52
  define_method("unmemoize_#{method_name}") do
41
53
  unmemoize(method_name)
42
54
  end
55
+ send(original_visibility, "unmemoize_#{method_name}")
43
56
 
44
- define_method("#{method_name}=") do |value|
45
- memoize(method_name, value)
57
+ if original_visibility != :private
58
+ define_method("#{method_name}=") do |value|
59
+ memoize(method_name, value)
60
+ end
61
+ send(original_visibility, "#{method_name}=")
46
62
  end
47
63
  end
48
64
  end
@@ -1,3 +1,3 @@
1
1
  module Mem
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -65,5 +65,41 @@ describe Mem do
65
65
  object.memoized_table.should == { a!: [1, 2] }
66
66
  end
67
67
  end
68
+
69
+ context "with non-public method name" do
70
+ let(:klass) do
71
+ Class.new do
72
+ include Mem
73
+
74
+ def a
75
+ end
76
+ memoize :a
77
+
78
+ protected
79
+
80
+ def b
81
+ end
82
+ memoize :b
83
+
84
+ private
85
+
86
+ def c
87
+ end
88
+ memoize :c
89
+ end
90
+ end
91
+
92
+ it 'keeps method visibilities' do
93
+ expect(klass.public_instance_methods).to include(:a)
94
+ expect(klass.protected_instance_methods).to include(:b)
95
+ expect(klass.private_instance_methods).to include(:c)
96
+ end
97
+
98
+ it 'does not define setter of private method' do
99
+ expect(klass.instance_methods).to include(:a=)
100
+ expect(klass.instance_methods).to include(:b=)
101
+ expect(klass.instance_methods).not_to include(:c=)
102
+ end
103
+ end
68
104
  end
69
105
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-27 00:00:00.000000000 Z
11
+ date: 2014-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler