-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed ActiveSupport::Memoizable API to extend since it mainly adds …
…the memoize class method
- Loading branch information
Showing
5 changed files
with
67 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
module ActiveSupport | ||
module Memoizable | ||
def self.included(base) #:nodoc: | ||
base.extend(ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
def memoize(*symbols) | ||
symbols.each do |symbol| | ||
original_method = "unmemoized_#{symbol}" | ||
memoized_ivar = "@#{symbol}" | ||
raise "Already memoized #{symbol}" if instance_methods.map(&:to_s).include?(original_method) | ||
|
||
alias_method original_method, symbol | ||
class_eval <<-EOS, __FILE__, __LINE__ | ||
def #{symbol}(reload = false) | ||
if !reload && defined? #{memoized_ivar} | ||
#{memoized_ivar} | ||
else | ||
#{memoized_ivar} = #{original_method}.freeze | ||
end | ||
module Memoizable #:nodoc: | ||
def self.extended(obj) | ||
klass = obj.respond_to?(:class_eval) ? obj : obj.metaclass | ||
klass.class_eval do | ||
def freeze | ||
methods.each do |method| | ||
if m = method.to_s.match(/^unmemoized_(.*)/) | ||
send(m[1]) | ||
end | ||
EOS | ||
end | ||
super | ||
end | ||
end | ||
end | ||
|
||
def freeze | ||
methods.each do |method| | ||
if m = method.to_s.match(/\Aunmemoized_(.*)/) | ||
send(m[1]) | ||
end | ||
def memoize(*symbols) | ||
symbols.each do |symbol| | ||
original_method = "unmemoized_#{symbol}" | ||
memoized_ivar = "@#{symbol}" | ||
|
||
klass = respond_to?(:class_eval) ? self : self.metaclass | ||
raise "Already memoized #{symbol}" if klass.instance_methods.map(&:to_s).include?(original_method) | ||
|
||
klass.class_eval <<-EOS, __FILE__, __LINE__ | ||
alias_method :#{original_method}, :#{symbol} | ||
def #{symbol}(reload = false) | ||
if !reload && defined? #{memoized_ivar} | ||
#{memoized_ivar} | ||
else | ||
#{memoized_ivar} = #{original_method}.freeze | ||
end | ||
end | ||
EOS | ||
end | ||
super | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ef6f662
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wow.
this includes my :comment saying “it should have a force_reload param”
ef6f662
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wow.
this includes my :comment saying “it should have a force_reload param”
ef6f662
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really want to accept args too, but it doesn’t mix well with reload. Too bad 1.8 does not support “method(*args, reload = false)”
ef6f662
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really want to accept args too, but it doesn’t mix well with reload. Too bad 1.8 does not support “method(*args, reload = false)”