memoized 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ab7a8649195530ae1e3ace893313e3731c1dcf3af93f6ebeaebad4a715efa65
4
- data.tar.gz: 543fd2584dbf7a34fd45c23394888cf8935a8f93f93be9a293b684e1054c3eed
3
+ metadata.gz: 794d162ed40844f311ed29ff37885d44930b416467b9fa9c49adb77646f6f44b
4
+ data.tar.gz: 59dcbf3a4cd03d2f0ed4bcab509758dd59a9fccdf0fe85999b325e18cb0d2948
5
5
  SHA512:
6
- metadata.gz: 13673e0131ff6560848e8dcac44ea746db4c18602b3fa68d3072dbefeaca95bf93ea1bbd4159ce49c8be30cd7361e616237c1ef61d19bc6e2d72efaba6a5d9fe
7
- data.tar.gz: 99f27fd8d150aaf3283d29cd159f16ed31beb13c6a293338709f15397c562294caf55a971b7764847895919aaf46c1a87ea77c123f2680691b1765b361b129b8
6
+ metadata.gz: bd346b36f77cca85aa9cf29bb20d4d1f0f5358d409138a087be59675ba07dc65003d69d654c3901b3aaf97de14812a65356e1cb44e8cec5ffbebd45039266537
7
+ data.tar.gz: cc3d756d28efbeb07ca0ae2510fc46b077b59c1b8945f4752dd9f410105c6e4cbbe566d6a0b67c62772ae8ad5b3cddfa2ca821ba1bea8ae5be023ee165da4393
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- memoized (1.0.0)
4
+ memoized (1.0.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -22,8 +22,7 @@ module Memoized
22
22
 
23
23
  arity = instance_method(unmemoized_method).arity
24
24
 
25
- case arity
26
- when 0
25
+ if arity == 0
27
26
  module_eval(<<-RUBY)
28
27
  def #{method_name}()
29
28
  #{memoized_ivar_name} ||= [#{unmemoized_method}()]
@@ -31,7 +30,7 @@ module Memoized
31
30
  end
32
31
  RUBY
33
32
 
34
- when -1
33
+ elsif arity == -1
35
34
  module_eval(<<-RUBY)
36
35
  def #{method_name}(*args)
37
36
  #{memoized_ivar_name} ||= {}
@@ -43,18 +42,36 @@ module Memoized
43
42
  end
44
43
  RUBY
45
44
 
46
- else
47
- arg_names = (0..(arity - 1)).map { |i| "arg#{i}" }
45
+ elsif arity < -1
46
+ # For Ruby methods that take a variable number of arguments,
47
+ # Method#arity returns -n-1, where n is the number of required arguments
48
+ required_arg_names = (1..(-arity - 1)).map { |i| "arg#{i}" }
49
+ required_args_ruby = required_arg_names.join(', ')
50
+
51
+ module_eval(<<-RUBY)
52
+ def #{method_name}(#{required_args_ruby}, *optional_args)
53
+ all_args = [#{required_args_ruby}, *optional_args]
54
+ #{memoized_ivar_name} ||= {}
55
+ if #{memoized_ivar_name}.has_key?(all_args)
56
+ #{memoized_ivar_name}[all_args]
57
+ else
58
+ #{memoized_ivar_name}[all_args] = #{unmemoized_method}(*all_args)
59
+ end
60
+ end
61
+ RUBY
62
+
63
+ else # positive arity
64
+ arg_names = (1..arity).map { |i| "arg#{i}" }
48
65
  args_ruby = arg_names.join(', ')
49
66
 
50
67
  module_eval(<<-RUBY)
51
68
  def #{method_name}(#{args_ruby})
52
- args = [#{args_ruby}]
69
+ all_args = [#{args_ruby}]
53
70
  #{memoized_ivar_name} ||= {}
54
- if #{memoized_ivar_name}.has_key?(args)
55
- #{memoized_ivar_name}[args]
71
+ if #{memoized_ivar_name}.has_key?(all_args)
72
+ #{memoized_ivar_name}[all_args]
56
73
  else
57
- #{memoized_ivar_name}[args] = #{unmemoized_method}(#{args_ruby})
74
+ #{memoized_ivar_name}[all_args] = #{unmemoized_method}(#{args_ruby})
58
75
  end
59
76
  end
60
77
  RUBY
@@ -1,3 +1,3 @@
1
1
  module Memoized
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -145,6 +145,48 @@ describe Memoized do
145
145
 
146
146
  end
147
147
 
148
+ context 'for methods with a required and an optional arg' do
149
+
150
+ class ArityRequiredAndOptional < MemoizedSpecClass
151
+ def foo(a, b = 'default')
152
+ return [a, b]
153
+ end
154
+
155
+ memoize :foo
156
+ end
157
+
158
+ it 'creates a memoized method with a arity of -2' do
159
+ expect(ArityRequiredAndOptional.instance_method(:foo).arity).to eq(-2)
160
+ end
161
+
162
+ it "preserves the optional arg's default value" do
163
+ instance = ArityRequiredAndOptional.new
164
+ expect(instance.foo('foo')).to eq ['foo', 'default']
165
+ end
166
+
167
+ end
168
+
169
+ context 'for methods with a required arg and splat args' do
170
+
171
+ class ArityArgAndOptional < MemoizedSpecClass
172
+ def foo(a, *args)
173
+ return [a, args]
174
+ end
175
+
176
+ memoize :foo
177
+ end
178
+
179
+ it 'creates a memoized method with a arity of -2' do
180
+ expect(ArityArgAndOptional.instance_method(:foo).arity).to eq(-2)
181
+ end
182
+
183
+ it "passes the splat args to the memoized method" do
184
+ instance = ArityArgAndOptional.new
185
+ expect(instance.foo('foo', 'bar', 'baz')).to eq ['foo', ['bar', 'baz']]
186
+ end
187
+
188
+ end
189
+
148
190
  end
149
191
 
150
192
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memoized
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Barun Singh
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-02-27 00:00:00.000000000 Z
12
+ date: 2019-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake