aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-06-15 02:52:12 -0700
committerJosé Valim <jose.valim@gmail.com>2011-06-15 02:52:12 -0700
commit6ab3da62083773cc507851694324cfb413f07ea8 (patch)
treefd4568516275ff60cfed9fd68354230d47567fa0 /activesupport
parent0438e92ae73544874ab9cd73d0a33e7a815c395b (diff)
parent9791e3e25221818f283e5e734772462b0d2d7d5d (diff)
downloadrails-6ab3da62083773cc507851694324cfb413f07ea8.tar.gz
rails-6ab3da62083773cc507851694324cfb413f07ea8.tar.bz2
rails-6ab3da62083773cc507851694324cfb413f07ea8.zip
Merge pull request #1707 from dmathieu/memoize
Allow last argument to be a boolean in memoized methods
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/memoizable.rb6
-rw-r--r--activesupport/test/memoizable_test.rb14
2 files changed, 19 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb
index 0a7bcd5bb8..279f1cf71f 100644
--- a/activesupport/lib/active_support/memoizable.rb
+++ b/activesupport/lib/active_support/memoizable.rb
@@ -79,7 +79,11 @@ module ActiveSupport
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
+ 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)
diff --git a/activesupport/test/memoizable_test.rb b/activesupport/test/memoizable_test.rb
index bceac1315b..7ffe132b80 100644
--- a/activesupport/test/memoizable_test.rb
+++ b/activesupport/test/memoizable_test.rb
@@ -96,6 +96,15 @@ class MemoizableTest < ActiveSupport::TestCase
end
memoize :fib
+ def add_or_subtract(i, j, add)
+ if add
+ i + j
+ else
+ i - j
+ end
+ end
+ memoize :add_or_subtract
+
def counter
@count ||= 0
@count += 1
@@ -199,6 +208,11 @@ class MemoizableTest < ActiveSupport::TestCase
assert_equal 13, @calculator.fib_calls
end
+ def test_memoization_with_boolean_arg
+ assert_equal 4, @calculator.add_or_subtract(2, 2, true)
+ assert_equal 2, @calculator.add_or_subtract(4, 2, false)
+ end
+
def test_object_memoization
[Company.new, Company.new, Company.new].each do |company|
company.extend ActiveSupport::Memoizable