Skip to content

Commit

Permalink
Merge pull request #27 from ArtemisInternet/fix-optional-value-not-su…
Browse files Browse the repository at this point in the history
…pported

Call abs on arity when extracting reload
  • Loading branch information
matthewrudy committed Oct 10, 2014
2 parents f5e3c99 + 051c56c commit 85b9f72
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/memoist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.memoist_eval(klass, *args, &block)
end

def self.extract_reload!(method, args)
if args.length == method.arity + 1 && (args.last == true || args.last == :reload)
if args.length == method.arity.abs + 1 && (args.last == true || args.last == :reload)
reload = args.pop
end
reload
Expand Down
42 changes: 42 additions & 0 deletions test/memoist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ def age

memoize :name, :age

def sleep(hours = 8)
@counter.call(:sleep)
hours
end
memoize :sleep

def sleep_calls
@counter.count(:sleep)
end

def update_attributes(options = {})
@counter.call(:update_attributes)
true
end
memoize :update_attributes

def update_attributes_calls
@counter.count(:update_attributes)
end

protected

def memoize_protected_test
Expand Down Expand Up @@ -167,6 +187,28 @@ def test_memoization
assert_equal 1, @person.name_calls
end

def test_memoize_with_optional_arguments
assert_equal 4, @person.sleep(4)
assert_equal 1, @person.sleep_calls

3.times { assert_equal 4, @person.sleep(4) }
assert_equal 1, @person.sleep_calls

3.times { assert_equal 4, @person.sleep(4, :reload) }
assert_equal 4, @person.sleep_calls
end

def test_memoize_with_options_hash
assert_equal true, @person.update_attributes(:age => 21, :name => 'James')
assert_equal 1, @person.update_attributes_calls

3.times { assert_equal true, @person.update_attributes(:age => 21, :name => 'James') }
assert_equal 1, @person.update_attributes_calls

3.times { assert_equal true, @person.update_attributes({:age => 21, :name => 'James'}, :reload) }
assert_equal 4, @person.update_attributes_calls
end

def test_memoization_with_punctuation
assert_equal true, @person.name?

Expand Down

0 comments on commit 85b9f72

Please sign in to comment.