Skip to content

Commit

Permalink
allow arguments for counter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjudd committed Aug 23, 2010
1 parent eded064 commit 118c68d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 0 additions & 2 deletions lib/method_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def cache_method(method_name, opts = {})
cached_instance_methods[method_name] = nil
if method_defined?(method_name)
if proxy.opts[:counter]
raise "counter only permitted on methods with arity 0" if instance_method(method_name).arity != 0
define_method "increment_#{method_name}", proxy.counter_method(:increment)
define_method "decrement_#{method_name}", proxy.counter_method(:decrement)
end
Expand Down Expand Up @@ -53,7 +52,6 @@ def cache_class_method(method_name, opts = {})
if class_method_defined?(method_name)
(class << self; self; end).module_eval do
if proxy.opts[:counter]
raise "counter only permitted on methods with arity 0" if instance_method(method_name).arity != 0
define_method "increment_#{method_name}", proxy.counter_method(:increment)
define_method "decrement_#{method_name}", proxy.counter_method(:decrement)
end
Expand Down
9 changes: 4 additions & 5 deletions lib/method_cache/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ def counter_method(method_name)
proxy = self # Need access to the proxy in the closure.

lambda do |*args|
if args.empty?
proxy.bind(self, []).send(method_name, 1)
else
amount = args.shift
proxy.bind(self, args).send(method_name, amount)
if args.last.kind_of?(Hash) and args.last.keys == [:by]
amount = args.last[:by]
args.pop
end
proxy.bind(self, args).send(method_name, amount || 1)
end
end

Expand Down
23 changes: 14 additions & 9 deletions test/method_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def foo(i)
end

cache_method :foo_count, :counter => true, :cache => :default
def foo_count
def foo_count(key)
100
end
end
Expand Down Expand Up @@ -139,16 +139,21 @@ class TestMethodCache < Test::Unit::TestCase
should 'cache counters' do
b = Baz.new

assert_equal 100, b.foo_count
b.increment_foo_count(42)
assert_equal 142, b.foo_count
b.decrement_foo_count(99)
assert_equal 43, b.foo_count
b.increment_foo_count
assert_equal 44, b.foo_count
assert_equal 100, b.foo_count(:bar)
b.increment_foo_count(:bar, :by => 42)
assert_equal 142, b.foo_count(:bar)
b.decrement_foo_count(:bar, :by => 99)
assert_equal 43, b.foo_count(:bar)
b.increment_foo_count(:bar)
assert_equal 44, b.foo_count(:bar)

assert_equal 100, b.foo_count(:baz)
b.increment_foo_count(:baz)
assert_equal 101, b.foo_count(:baz)
assert_equal 44, b.foo_count(:bar) # make sure :bar wasn't affected

assert_equal 0, Foo.zap
Foo.increment_zap(3)
Foo.increment_zap(:by => 3)
assert_equal 3, Foo.zap
Foo.decrement_zap
assert_equal 2, Foo.zap
Expand Down

0 comments on commit 118c68d

Please sign in to comment.