Skip to content

Commit

Permalink
Merge branch 'master' into flush-class-memoizations
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewrudy committed Jun 20, 2017
2 parents 0273051 + fb82fd1 commit 111b6c4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/memoist.rb
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 @@ -117,14 +127,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
32 changes: 32 additions & 0 deletions test/memoist_test.rb
Expand Up @@ -208,6 +208,31 @@ def all_types
end
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 @@ -303,6 +328,13 @@ def test_class_flush_cache_preserves_instances
assert_equal false, Book.instance_variable_defined?(:@_memoized_all_types)
assert_equal "My Life by Brian 'Fudge' Turmuck", @book.full_title
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

0 comments on commit 111b6c4

Please sign in to comment.