Skip to content

Commit b27be13

Browse files
committed
Always include the class name / memoization module id in method keys in order for allow for class inheritance with memoized method overrides
1 parent fdd8b8c commit b27be13

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/memery.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def fresh?(ttl)
7474

7575
# rubocop:disable Metrics/MethodLength
7676
def define_memoized_method!(klass, method_name, condition: nil, ttl: nil)
77+
# Include a suffix in the method key to differentiate between methods of the same name
78+
# being memoized throughout a class inheritance hierarchy
79+
method_key = "#{method_name}_#{klass.name || object_id}"
7780
original_visibility = method_visibility(klass, method_name)
7881

7982
define_method(method_name) do |*args, &block|
@@ -83,9 +86,9 @@ def define_memoized_method!(klass, method_name, condition: nil, ttl: nil)
8386

8487
cache_store = (@_memery_memoized_values ||= {})
8588
cache_key = if args.empty?
86-
method_name
89+
method_key
8790
else
88-
key_parts = [method_name, *args]
91+
key_parts = [method_key, *args]
8992
Memery.use_hashed_arguments ? key_parts.hash : key_parts
9093
end
9194
cache = cache_store[cache_key]

0 commit comments

Comments
 (0)