Class: KGB::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/kgb.rb

Constant Summary collapse

PADDING =
4

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Agent

Returns a new instance of Agent.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kgb.rb', line 20

def initialize(klass)
  @invocations = {}

  @class = klass
  @class.class_eval do
    include RCapture::Interceptable
  end

  @class.capture :methods => @class.instance_methods do |info|
    @invocations[info.method] ||= 0
    @invocations[info.method] += 1
  end
end

Instance Method Details

#reportObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kgb.rb', line 36

def report
  say
  say "#{@class}:"
  say

  if @invocations.size > 0
    max_length = @invocations.keys.map { |key| key.length }.max
    max_length = 6 if max_length < "method".length

    say "method" + " " * (max_length - 6 + PADDING) + "invocations"
    @invocations.sort { |a, b| b[1] <=> a[1] }.each do |method, times|
      say method.to_s + " " * (max_length - method.length + PADDING) + times.to_s
    end
  end
end