Skip to content

Allow memoization for instances whose classes include a module that includes a module prepending MemoWise #382

@JacobEvelyn

Description

@JacobEvelyn

As noted by @alpaca-tc in this comment, if we add these tests they fail:

diff --git a/spec/memo_wise_spec.rb b/spec/memo_wise_spec.rb
index 6e73d3a..a2355a1 100644
--- a/spec/memo_wise_spec.rb
+++ b/spec/memo_wise_spec.rb
@@ -365,18 +365,32 @@ RSpec.describe MemoWise do
           end
         end
 
+        let(:nested_module_with_initializer) do
+          Module.new do
+            include Module3
+            def initialize(*); end
+          end
+        end
+
         let(:klass_with_module_with_initializer) do
           Class.new do
             include Module3
           end
         end
 
+        let(:klass_with_nested_module_with_initializer) do
+          Class.new do
+            include Module4
+          end
+        end
+
         let(:instance) { klass.new }
 
         before(:each) do
           stub_const("Module1", module1)
           stub_const("Module2", module2)
           stub_const("Module3", module_with_initializer)
+          stub_const("Module4", nested_module_with_initializer)
         end
 
         it "memoizes inherited methods separately" do
@@ -402,6 +416,14 @@ RSpec.describe MemoWise do
           expect(instance.module1_method_counter).to eq(1)
         end
 
+        it "can memoize klass with nested module with initializer" do
+          instance = klass_with_nested_module_with_initializer.new(true)
+          expect { instance.module1_method }.not_to raise_error
+
+          expect(Array.new(4) { instance.module1_method }).to all eq("module1_method")
+          expect(instance.module1_method_counter).to eq(1)
+        end
+
         it "can reset klass with initializer" do
           instance = klass_with_initializer.new(true)
           expect { instance.reset_memo_wise }.not_to raise_error
@@ -411,6 +433,11 @@ RSpec.describe MemoWise do
           instance = klass_with_module_with_initializer.new(true)
           expect { instance.reset_memo_wise }.not_to raise_error
         end
+
+        it "can reset klass with nested module with initializer" do
+          instance = klass_with_nested_module_with_initializer.new(true)
+          expect { instance.reset_memo_wise }.not_to raise_error
+        end
       end
 
       context "when the class, its superclass, and its module all memoize methods" do

If anyone runs into this issue, please 👍 or comment on this issue to help us prioritize resolving it.

While fixing this, we should see if there are ways to more holistically handle these types of inheritance cases rather than supporting just the cases people file issues for.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions