Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always set memo_wise hash if not set for some reason #321

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/memo_wise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def inherited(subclass)
when MemoWise::InternalAPI::NONE
klass.module_eval <<~HEREDOC, __FILE__, __LINE__ + 1
def #{method_name}
@_memo_wise ||= {}
@_memo_wise.fetch(:#{method_name}) do
@_memo_wise[:#{method_name}] = #{original_memo_wised_name}
end
Expand All @@ -190,6 +191,7 @@ def #{method_name}
key = method.parameters.first.last
klass.module_eval <<~HEREDOC, __FILE__, __LINE__ + 1
def #{method_name}(#{MemoWise::InternalAPI.args_str(method)})
@_memo_wise ||= {}
_memo_wise_hash = (@_memo_wise[:#{method_name}] ||= {})
_memo_wise_hash.fetch(#{key}) do
_memo_wise_hash[#{key}] = #{original_memo_wised_name}(#{MemoWise::InternalAPI.call_str(method)})
Expand All @@ -211,6 +213,7 @@ def #{method_name}(#{MemoWise::InternalAPI.args_str(method)})
end
klass.module_eval <<~HEREDOC, __FILE__, __LINE__ + 1
def #{method_name}(#{MemoWise::InternalAPI.args_str(method)})
@_memo_wise ||= {}
_memo_wise_hash = (@_memo_wise[:#{method_name}] ||= {})
#{layers.join("\n ")}
end
Expand All @@ -220,6 +223,7 @@ def #{method_name}(#{MemoWise::InternalAPI.args_str(method)})
# { method_name: { args => { kwargs => memoized_value } } }
klass.module_eval <<~HEREDOC, __FILE__, __LINE__ + 1
def #{method_name}(*args, **kwargs)
@_memo_wise ||= {}
_memo_wise_hash = (@_memo_wise[:#{method_name}] ||= {})
_memo_wise_kwargs_hash = _memo_wise_hash.fetch(args) do
_memo_wise_hash[args] = {}
Expand All @@ -232,6 +236,7 @@ def #{method_name}(*args, **kwargs)
else # MemoWise::InternalAPI::SPLAT, MemoWise::InternalAPI::DOUBLE_SPLAT
klass.module_eval <<~HEREDOC, __FILE__, __LINE__ + 1
def #{method_name}(#{MemoWise::InternalAPI.args_str(method)})
@_memo_wise ||= {}
_memo_wise_hash = (@_memo_wise[:#{method_name}] ||= {})
_memo_wise_key = #{MemoWise::InternalAPI.key_str(method)}
_memo_wise_hash.fetch(_memo_wise_key) do
Expand Down Expand Up @@ -552,6 +557,7 @@ def reset_memo_wise(method_name = nil, *args, **kwargs)
raise ArgumentError, "Provided args when method_name = nil" unless args.empty?
raise ArgumentError, "Provided kwargs when method_name = nil" unless kwargs.empty?

@_memo_wise ||= {}
@_memo_wise.clear
return
end
Expand Down
17 changes: 17 additions & 0 deletions spec/memo_wise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ def module2_method
end
end

let(:klass_with_initializer) do
Class.new do
include Module1
def initialize(*); end
end
end

let(:instance) { klass.new }

before(:each) do
Expand All @@ -364,6 +371,16 @@ def module2_method
expect(Array.new(4) { instance.module2_method }).to all eq("module2_method")
expect(instance.module2_method_counter).to eq(1)
end

it "can memoize klass with initializer" do
instance = klass_with_initializer.new(true)
expect { instance.module1_method }.not_to raise_error
end

it "can reset klass with initializer" do
instance = klass_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
Expand Down