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

Fix undefined memoized_methods error raised when a parent class has not call memoize #68

Merged
merged 1 commit into from
Jun 20, 2017
Merged
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
18 changes: 10 additions & 8 deletions lib/memoist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

module Memoist

def self.extended(extender)
Memoist.memoist_eval(extender) do
unless singleton_class.method_defined?(:memoized_methods)
def self.memoized_methods
@_memoized_methods ||= []
end
end
end
end

def self.memoized_ivar_for(method_name, identifier=nil)
"@#{memoized_prefix(identifier)}_#{escape_punctuation(method_name)}"
end
Expand Down Expand Up @@ -116,14 +126,6 @@ def memoize(*method_names)
identifier = method_names.pop[:identifier]
end

Memoist.memoist_eval(self) do
unless singleton_class.method_defined?(:memoized_methods)
def self.memoized_methods
@_memoized_methods ||= []
end
end
end

method_names.each do |method_name|
unmemoized_method = Memoist.unmemoized_method_for(method_name, identifier)
memoized_ivar = Memoist.memoized_ivar_for(method_name, identifier)
Expand Down
34 changes: 34 additions & 0 deletions test/memoist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ def counter
memoize :counter
end

class Abb
extend Memoist

def run(*args)
flush_cache if respond_to?(:flush_cache)
execute
end

def execute
some_method
end

def some_method
# Override this
end
end

class Bbb < Abb

def some_method
:foo
end
memoize :some_method
end



def setup
@person = Person.new
@calculator = Calculator.new
Expand Down Expand Up @@ -256,6 +283,13 @@ def test_flush_cache
assert_equal 2, @calculator.counter
end

def test_flush_cache_in_child_class
x = Bbb.new

# This should not throw error
x.run
end

def test_unmemoize_all
assert_equal 1, @calculator.counter

Expand Down