Permalink
Browse files

add an optional identifier to memoize

  • Loading branch information...
1 parent af13380 commit a10c075e9f46efa5da0eb0943533d7c8f1f33ca3 @matthewrudy committed Mar 1, 2013
Showing with 85 additions and 48 deletions.
  1. +63 −47 lib/memoist.rb
  2. +22 −1 test/memoist_test.rb
View
@@ -3,8 +3,16 @@
module Memoist
- def self.memoized_ivar_for(symbol)
- "@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
+ def self.memoized_ivar_for(method_name, identifier=nil)
+ ["@_memoized", identifier, escape_punctuation(method_name.to_s)].compact.join("_")
+ end
+
+ def self.unmemoized_method_for(method_name, identifier=nil)
+ ["_unmemoized", identifier, method_name].compact.join("_").to_sym
+ end
+
+ def self.escape_punctuation(string)
+ string.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')
end
module InstanceMethods
@@ -63,52 +71,60 @@ def flush_cache(*syms)
end
def memoize(*symbols)
+ if symbols.last.is_a?(Hash)
+ identifier = symbols.pop[:identifier]
+ end
+
symbols.each do |symbol|
- original_method = :"_unmemoized_#{symbol}"
- memoized_ivar = Memoist.memoized_ivar_for(symbol)
-
- class_eval <<-EOS, __FILE__, __LINE__ + 1
- include InstanceMethods # include InstanceMethods
- #
- if method_defined?(:#{original_method}) # if method_defined?(:_unmemoized_mime_type)
- raise "Already memoized #{symbol}" # raise "Already memoized mime_type"
- end # end
- alias #{original_method} #{symbol} # alias _unmemoized_mime_type mime_type
- #
- if instance_method(:#{symbol}).arity == 0 # if instance_method(:mime_type).arity == 0
- def #{symbol}(reload = false) # def mime_type(reload = false)
- if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? # if reload || !defined?(@_memoized_mime_type) || @_memoized_mime_type.empty?
- #{memoized_ivar} = [#{original_method}] # @_memoized_mime_type = [_unmemoized_mime_type]
- end # end
- #{memoized_ivar}[0] # @_memoized_mime_type[0]
- end # end
- else # else
- def #{symbol}(*args) # def mime_type(*args)
- #{memoized_ivar} ||= {} unless frozen? # @_memoized_mime_type ||= {} unless frozen?
- args_length = method(:#{original_method}).arity # args_length = method(:_unmemoized_mime_type).arity
- if args.length == args_length + 1 && # if args.length == args_length + 1 &&
- (args.last == true || args.last == :reload) # (args.last == true || args.last == :reload)
- reload = args.pop # reload = args.pop
- end # end
- #
- if defined?(#{memoized_ivar}) && #{memoized_ivar} # if defined?(@_memoized_mime_type) && @_memoized_mime_type
- if !reload && #{memoized_ivar}.has_key?(args) # if !reload && @_memoized_mime_type.has_key?(args)
- #{memoized_ivar}[args] # @_memoized_mime_type[args]
- elsif #{memoized_ivar} # elsif @_memoized_mime_type
- #{memoized_ivar}[args] = #{original_method}(*args) # @_memoized_mime_type[args] = _unmemoized_mime_type(*args)
- end # end
- else # else
- #{original_method}(*args) # _unmemoized_mime_type(*args)
- end # end
- end # end
- end # end
- #
- if private_method_defined?(#{original_method.inspect}) # if private_method_defined?(:_unmemoized_mime_type)
- private #{symbol.inspect} # private :mime_type
- elsif protected_method_defined?(#{original_method.inspect}) # elsif protected_method_defined?(:_unmemoized_mime_type)
- protected #{symbol.inspect} # protected :mime_type
- end # end
- EOS
+ original_method = Memoist.unmemoized_method_for(symbol, identifier)
+ memoized_ivar = Memoist.memoized_ivar_for(symbol, identifier)
+
+ class_eval do
+ include InstanceMethods
+
+ if method_defined?(original_method)
+ raise "Already memoized #{symbol}"
+ end
+ alias_method original_method, symbol
+
+ if instance_method(symbol).arity == 0
+ module_eval <<-EOS, __FILE__, __LINE__ + 1
+ def #{symbol}(reload = false) # def mime_type(reload = false)
+ if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? # if reload || !defined?(@_memoized_mime_type) || @_memoized_mime_type.empty?
+ #{memoized_ivar} = [#{original_method}] # @_memoized_mime_type = [_unmemoized_mime_type]
+ end # end
+ #{memoized_ivar}[0] # @_memoized_mime_type[0]
+ end # end
+ EOS
+ else
+ module_eval <<-EOS, __FILE__, __LINE__ + 1
+ def #{symbol}(*args) # def mime_type(*args)
+ #{memoized_ivar} ||= {} unless frozen? # @_memoized_mime_type ||= {} unless frozen?
+ args_length = method(:#{original_method}).arity # args_length = method(:_unmemoized_mime_type).arity
+ if args.length == args_length + 1 && # if args.length == args_length + 1 &&
+ (args.last == true || args.last == :reload) # (args.last == true || args.last == :reload)
+ reload = args.pop # reload = args.pop
+ end # end
+ #
+ if defined?(#{memoized_ivar}) && #{memoized_ivar} # if defined?(@_memoized_mime_type) && @_memoized_mime_type
+ if !reload && #{memoized_ivar}.has_key?(args) # if !reload && @_memoized_mime_type.has_key?(args)
+ #{memoized_ivar}[args] # @_memoized_mime_type[args]
+ elsif #{memoized_ivar} # elsif @_memoized_mime_type
+ #{memoized_ivar}[args] = #{original_method}(*args) # @_memoized_mime_type[args] = _unmemoized_mime_type(*args)
+ end # end
+ else # else
+ #{original_method}(*args) # _unmemoized_mime_type(*args)
+ end # end
+ end # end
+ EOS
+ end
+
+ if private_method_defined?(original_method)
+ private symbol
+ elsif protected_method_defined?(original_method)
+ protected symbol
+ end
+ end
end
end
end
View
@@ -5,10 +5,11 @@ class MemoistTest < Test::Unit::TestCase
class Person
extend Memoist
- attr_reader :name_calls, :age_calls, :is_developer_calls, :name_query_calls
+ attr_reader :name_calls, :student_name_calls, :age_calls, :is_developer_calls, :name_query_calls
def initialize
@name_calls = 0
+ @student_name_calls = 0
@age_calls = 0
@is_developer_calls = 0
@name_query_calls = 0
@@ -53,6 +54,14 @@ def is_developer?
memoize :is_developer?
end
+ class Student < Person
+ def name
+ @student_name_calls += 1
+ "Student #{super}"
+ end
+ memoize :name, :identifier => :student
+ end
+
class Company
attr_reader :name_calls
def initialize
@@ -259,6 +268,18 @@ def test_double_memoization
assert_raise(RuntimeError) { company.memoize :name }
end
+ def test_double_memoization_with_identifier
+ assert_nothing_raised { Person.memoize :name, :identifier => :again }
+ end
+
+ def test_memoization_with_a_subclass
+ student = Student.new
+ student.name
+ student.name
+ assert_equal 1, student.student_name_calls
+ assert_equal 1, student.name_calls
+ end
+
def test_protected_method_memoization
person = Person.new

0 comments on commit a10c075

Please sign in to comment.