Class: MethodCallCount

Inherits:
Object
  • Object
show all
Defined in:
lib/method_call_count.rb,
lib/method_call_count/version.rb,
lib/method_call_count/callable_stub.rb

Defined Under Namespace

Classes: CallableStub, Error

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.enable(klass) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/method_call_count.rb', line 9

def self.enable(klass)
  return if @@initialized.include?(klass)
  @@initialized.push(klass)
  klass.prepend Module.new{
    (klass.instance_methods - klass.superclass.instance_methods - [:enable]).each do |method|
      define_method(method) do |*args|
        @called[method] = (@called[method] || 0) + 1
        super(*args)
      end
    end

    def initialize(*args)
      @called = Hash.new{|h, k| h[k] = 0}
      super(*args)
    end

    def times_called
      @called.dup
    end
  }
end