2009-04-22 20:41:28 -04:00
|
|
|
require 'active_support/core_ext/object/metaclass'
|
2009-05-13 19:15:37 -04:00
|
|
|
require 'active_support/core_ext/module/aliasing'
|
2009-04-22 20:41:28 -04:00
|
|
|
|
2008-07-14 20:50:32 -04:00
|
|
|
module ActiveSupport
|
2009-01-27 19:17:39 -05:00
|
|
|
module SafelyMemoizable
|
|
|
|
def safely_memoize(*symbols)
|
|
|
|
symbols.each do |symbol|
|
|
|
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
|
|
def #{symbol}(*args)
|
|
|
|
memoized = @_memoized_#{symbol} || ::ActiveSupport::ConcurrentHash.new
|
|
|
|
memoized[args] ||= memoized_#{symbol}(*args)
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-22 11:26:44 -04:00
|
|
|
module Memoizable
|
2008-12-03 11:57:08 -05:00
|
|
|
def self.memoized_ivar_for(symbol)
|
2008-10-05 18:24:52 -04:00
|
|
|
"@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
|
|
|
|
end
|
|
|
|
|
2008-12-03 11:57:08 -05:00
|
|
|
module InstanceMethods
|
2008-07-22 11:26:44 -04:00
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
|
|
|
unless base.method_defined?(:freeze_without_memoizable)
|
|
|
|
alias_method_chain :freeze, :memoizable
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def freeze_with_memoizable
|
2008-08-13 20:22:38 -04:00
|
|
|
memoize_all unless frozen?
|
|
|
|
freeze_without_memoizable
|
|
|
|
end
|
|
|
|
|
|
|
|
def memoize_all
|
2008-12-03 11:57:08 -05:00
|
|
|
prime_cache ".*"
|
|
|
|
end
|
|
|
|
|
|
|
|
def unmemoize_all
|
|
|
|
flush_cache ".*"
|
|
|
|
end
|
|
|
|
|
|
|
|
def prime_cache(*syms)
|
|
|
|
syms.each do |sym|
|
|
|
|
methods.each do |m|
|
|
|
|
if m.to_s =~ /^_unmemoized_(#{sym})/
|
|
|
|
if method(m).arity == 0
|
|
|
|
__send__($1)
|
|
|
|
else
|
|
|
|
ivar = ActiveSupport::Memoizable.memoized_ivar_for($1)
|
|
|
|
instance_variable_set(ivar, {})
|
|
|
|
end
|
2008-08-07 15:55:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-08-13 20:22:38 -04:00
|
|
|
end
|
2008-07-28 00:45:55 -04:00
|
|
|
|
2008-12-03 11:57:08 -05:00
|
|
|
def flush_cache(*syms, &block)
|
|
|
|
syms.each do |sym|
|
|
|
|
methods.each do |m|
|
|
|
|
if m.to_s =~ /^_unmemoized_(#{sym})/
|
|
|
|
ivar = ActiveSupport::Memoizable.memoized_ivar_for($1)
|
|
|
|
instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
|
|
|
|
end
|
2008-08-13 20:22:38 -04:00
|
|
|
end
|
|
|
|
end
|
2008-07-22 11:26:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-18 16:32:28 -04:00
|
|
|
def memoize(*symbols)
|
|
|
|
symbols.each do |symbol|
|
2008-08-13 20:22:38 -04:00
|
|
|
original_method = :"_unmemoized_#{symbol}"
|
2008-12-03 11:57:08 -05:00
|
|
|
memoized_ivar = ActiveSupport::Memoizable.memoized_ivar_for(symbol)
|
2008-07-18 16:32:28 -04:00
|
|
|
|
2009-01-22 17:18:10 -05:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
2008-12-28 14:48:05 -05:00
|
|
|
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?
|
2009-04-08 20:32:19 -04:00
|
|
|
#{memoized_ivar} = [#{original_method}] # @_memoized_mime_type = [_unmemoized_mime_type]
|
2008-12-28 14:48:05 -05:00
|
|
|
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?
|
|
|
|
reload = args.pop if args.last == true || args.last == :reload # reload = args.pop if args.last == true || args.last == :reload
|
|
|
|
#
|
|
|
|
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
|
2009-04-08 20:32:19 -04:00
|
|
|
#{memoized_ivar}[args] = #{original_method}(*args) # @_memoized_mime_type[args] = _unmemoized_mime_type(*args)
|
2008-12-28 14:48:05 -05:00
|
|
|
end # end
|
|
|
|
else # else
|
|
|
|
#{original_method}(*args) # _unmemoized_mime_type(*args)
|
|
|
|
end # end
|
|
|
|
end # end
|
2009-03-06 09:40:54 -05:00
|
|
|
end # end
|
|
|
|
#
|
|
|
|
if private_method_defined?(#{original_method.inspect}) # if private_method_defined?(:_unmemoized_mime_type)
|
|
|
|
private #{symbol.inspect} # private :mime_type
|
2008-12-28 14:48:05 -05:00
|
|
|
end # end
|
2008-07-18 16:32:28 -04:00
|
|
|
EOS
|
2008-07-14 20:50:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|