1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
require 'singleton'
module Spy
# Manages all the spies
class Agency
include Singleton
# @private
def initialize
clear!
end
# given a spy ID it will return the associated spy
# @param id [Integer] spy object id
# @return [Nil, Subroutine, Constant, Double]
def find(id)
@spies[id]
end
# Record that a spy was initialized and hooked
# @param spy [Subroutine, Constant, Double]
# @return [spy]
def recruit(spy)
raise AlreadyStubbedError if @spies[spy.object_id]
check_spy!(spy)
@spies[spy.object_id] = spy
end
# remove spy from the records
# @param spy [Subroutine, Constant, Double]
# @return [spy]
def retire(spy)
raise NoSpyError unless @spies[spy.object_id]
@spies.delete(spy.object_id)
end
# checks to see if a spy is hooked
# @param spy [Subroutine, Constant, Double]
# @return [Boolean]
def active?(spy)
check_spy!(spy)
@spies.has_key?(spy.object_id)
end
# unhooks all spies and clears records
# @return [self]
def dissolve!
@spies.values.each do |spy|
spy.unhook if spy.respond_to?(:unhook)
end
clear!
end
# clears records
# @return [self]
def clear!
@spies = {}
self
end
# returns all the spies that have been initialized since the creation of
# this agency
# @return [Array<Subroutine, Constant, Double>]
def spies
@spies.values
end
private
def check_spy!(spy)
raise ArgumentError, "#{spy}, was not a spy" unless spy.is_a?(Base)
end
end
end
|