aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTarmo Tänav <tarmo@itech.ee>2008-07-14 20:23:23 -0500
committerJoshua Peek <josh@joshpeek.com>2008-07-14 20:23:23 -0500
commit911c2c381347ffb04615896ee6afe45277eeb103 (patch)
tree999da917148986101f8063b9109275535e138563
parent001c8beb4d0999a858a8b52ad511ee1251cc3517 (diff)
downloadrails-911c2c381347ffb04615896ee6afe45277eeb103.tar.gz
rails-911c2c381347ffb04615896ee6afe45277eeb103.tar.bz2
rails-911c2c381347ffb04615896ee6afe45277eeb103.zip
Some performance tweaks to ActiveSupport::Memoizable
Signed-off-by: Joshua Peek <josh@joshpeek.com>
-rw-r--r--activesupport/lib/active_support/memoizable.rb6
-rw-r--r--activesupport/test/memoizable_test.rb4
2 files changed, 8 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb
index 65feca363a..5af50df023 100644
--- a/activesupport/lib/active_support/memoizable.rb
+++ b/activesupport/lib/active_support/memoizable.rb
@@ -7,10 +7,12 @@ module ActiveSupport
module ClassMethods
def memoize(symbol)
original_method = "_unmemoized_#{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 instance_variable_defined?(:@#{symbol})
+ if defined? @#{symbol}
@#{symbol}
else
@#{symbol} = #{original_method}
@@ -22,7 +24,7 @@ module ActiveSupport
def freeze
methods.each do |method|
- if m = method.to_s.match(/^_unmemoized_(.*)/)
+ if m = method.to_s.match(/\A_unmemoized_(.*)/)
send(m[1]).freeze
end
end
diff --git a/activesupport/test/memoizable_test.rb b/activesupport/test/memoizable_test.rb
index 1b6cec2b2f..fc24a2942d 100644
--- a/activesupport/test/memoizable_test.rb
+++ b/activesupport/test/memoizable_test.rb
@@ -41,5 +41,9 @@ uses_mocha 'Memoizable' do
person.freeze
assert_equal nil, person.age
end
+
+ def test_double_memoization
+ assert_raise(RuntimeError) { Person.memoize :name }
+ end
end
end