aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/memoizable.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-07-16 17:45:28 -0500
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-07-16 17:45:28 -0500
commit73ade4fe100ae7f48c5f95dcdf7067f6a0cd51fa (patch)
tree2f1601292978a844226e16f3cef428ed760b9eb5 /activesupport/lib/active_support/memoizable.rb
parent8fe01de2e8753d045408ecde3178ab4e9192bf9a (diff)
parent90c930f45c5c6766306929241462ffff8f67b86e (diff)
downloadrails-73ade4fe100ae7f48c5f95dcdf7067f6a0cd51fa.tar.gz
rails-73ade4fe100ae7f48c5f95dcdf7067f6a0cd51fa.tar.bz2
rails-73ade4fe100ae7f48c5f95dcdf7067f6a0cd51fa.zip
Resolved conflict
Diffstat (limited to 'activesupport/lib/active_support/memoizable.rb')
-rw-r--r--activesupport/lib/active_support/memoizable.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb
new file mode 100644
index 0000000000..d06250171a
--- /dev/null
+++ b/activesupport/lib/active_support/memoizable.rb
@@ -0,0 +1,35 @@
+module ActiveSupport
+ module Memoizable
+ def self.included(base) #:nodoc:
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def memoize(symbol)
+ original_method = "_unmemoized_#{symbol}"
+ memoized_ivar = "@_memoized_#{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}
+ if defined? #{memoized_ivar}
+ #{memoized_ivar}
+ else
+ #{memoized_ivar} = #{original_method}
+ end
+ end
+ EOS
+ end
+ end
+
+ def freeze
+ methods.each do |method|
+ if m = method.to_s.match(/\A_unmemoized_(.*)/)
+ send(m[1]).freeze
+ end
+ end
+ super
+ end
+ end
+end