class Module

Public Instance Methods

call_site(depth=1) click to toggle source
# File lib/spy_on.rb, line 25
def call_site(depth=1)
  paths = caller(2).map { |path| path =~ %r\(eval\)/ ? path : File.expand_path(path).sub(%r#{File.expand_path Dir.pwd}/, '.') }
  our = paths.reject { |path| path =~ %rvendor.rails|rubygems/ }
  return ["#{our.first} via "] + paths.first(depth)
end
print_tally(tallies) click to toggle source
spy(msg, old_msg = nil, depth = 1) click to toggle source
# File lib/spy_on.rb, line 46
def spy msg, old_msg = nil, depth = 1
  puts "Spying on #{self}##{msg}"
  old_msg ||= "old_#{normal_to_test(msg.to_s).sub(/^test_/, '')}"
  alias_method old_msg, msg

  class_eval "
    @@tally_done = false
    @@tally ||= Hash.new { |h,k| h.safe_asgn(k, Hash.new(0)) }
    at_exit { at_exit { @@tally_done = true; print_tally @@tally if @@tally; @@tally = nil } }
    def #{msg}(*args, &block)
      unless @@tally_done then
        site = self.class.call_site(#{depth})
        x = @@tally.safe_idx('#{self}.#{msg}')
        x.safe_asgn(site, x.safe_idx(site) + 1)
      end
      #{old_msg}(*args, &block)
    end "
end
spy_cm(*args) click to toggle source
# File lib/spy_on.rb, line 65
def spy_cm *args
  this_sucks.spy *args
end
spy_on(*msgs) click to toggle source
# File lib/spy_on.rb, line 69
def spy_on *msgs
  msgs.each do |msg|
    spy msg
  end
end
spy_on_cm(*msgs) click to toggle source
# File lib/spy_on.rb, line 75
def spy_on_cm *msgs
  this_sucks.spy_on *msgs
end
this_sucks() click to toggle source
# File lib/spy_on.rb, line 79
def this_sucks
  class << self; self; end
end