Skip to content

Commit 5c21f47

Browse files
committed
Use memoist_reload flag for all methods
1 parent 45fb04a commit 5c21f47

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/memoist.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def self.extract_reload!(method, args)
6363
# We don't want to extract out the last argument in this case.
6464
# Also, if the :memoist_reload flag is passed, we use that
6565
# irrespective of the arity.
66-
if method.arity.negative? && args.length > 0 && args.last.is_a?(Hash)
66+
if args.length > 0 && args.last.is_a?(Hash)
6767
named_args_hash = args.last
6868
reload = named_args_hash.delete :memoist_reload
6969
# If no other named args were passed, that means memoist_reload is an
@@ -74,8 +74,10 @@ def self.extract_reload!(method, args)
7474
end
7575
return reload
7676
end
77+
return false if method.arity.negative?
7778

78-
if args.length == method.arity.abs + 1 && (args.last == true || args.last == :reload)
79+
# Legacy purposes, works only for methods with no optional/named args
80+
if args.length == method.arity + 1 && (args.last == true || args.last == :reload)
7981
reload = args.pop
8082
end
8183
reload

test/memoist_test.rb

+14-6
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def age
6767

6868
memoize :name, :age
6969

70-
def sleep(initial_delay, hours = 8, mins = 5)
70+
def sleep(hours, mins = 5, seconds: 0)
7171
@counter.call(:sleep)
72-
hours
72+
hours * 3600 + mins * 60 + seconds
7373
end
7474
memoize :sleep
7575

@@ -241,19 +241,25 @@ def test_memoization
241241

242242
3.times { assert_equal 'Josh', @person.name }
243243
assert_equal 1, @person.name_calls
244+
245+
3.times { assert_equal 'Josh', @person.name(memoist_reload: true) }
246+
assert_equal 4, @person.name_calls
244247
end
245248

246249
def test_memoize_with_optional_arguments
247-
assert_equal 4, @person.sleep(4, 4)
250+
assert_equal 15000, @person.sleep(4, 10)
248251
assert_equal 1, @person.sleep_calls
249252

250-
3.times { assert_equal 4, @person.sleep(4, 4) }
253+
3.times { assert_equal 15000, @person.sleep(4, 10) }
251254
assert_equal 1, @person.sleep_calls
252255

256+
3.times { assert_equal 15000, @person.sleep(4, 10, seconds: 0) }
257+
assert_equal 2, @person.sleep_calls
258+
253259
# There has been exactly one sleep call due to memoization
254260
# Now, with reload, we'll have 3 more calls
255-
3.times { assert_equal 10, @person.sleep(4, 10, memoist_reload: true) }
256-
assert_equal 4, @person.sleep_calls
261+
3.times { assert_equal 15000, @person.sleep(4, 10, memoist_reload: true) }
262+
assert_equal 5, @person.sleep_calls
257263
end
258264

259265
def test_memoize_with_options_hash
@@ -295,6 +301,8 @@ def test_reloadable
295301
assert_equal 2, @calculator.counter
296302
assert_equal 3, @calculator.counter(true)
297303
assert_equal 3, @calculator.counter
304+
assert_equal 4, @calculator.counter(memoist_reload: true)
305+
assert_equal 4, @calculator.counter
298306
end
299307

300308
def test_flush_cache

0 commit comments

Comments
 (0)