class Spy::Agency

Manages all the spies

Public Class Methods

new() click to toggle source

@private

# File lib/spy/agency.rb, line 9
def initialize
  clear!
end

Public Instance Methods

active?(spy) click to toggle source

checks to see if a spy is hooked @param spy [Subroutine, Constant, Double] @return [Boolean]

# File lib/spy/agency.rb, line 40
def active?(spy)
  check_spy!(spy)
  @spies.has_key?(spy.object_id)
end
clear!() click to toggle source

clears records @return [self]

# File lib/spy/agency.rb, line 56
def clear!
  @spies = {}
  self
end
dissolve!() click to toggle source

unhooks all spies and clears records @return [self]

# File lib/spy/agency.rb, line 47
def dissolve!
  @spies.values.each do |spy|
    spy.unhook if spy.respond_to?(:unhook)
  end
  clear!
end
find(id) click to toggle source

given a spy ID it will return the associated spy @param id [Integer] spy object id @return [Nil, Subroutine, Constant, Double]

# File lib/spy/agency.rb, line 16
def find(id)
  @spies[id]
end
recruit(spy) click to toggle source

Record that a spy was initialized and hooked @param spy [Subroutine, Constant, Double] @return [spy]

# File lib/spy/agency.rb, line 23
def recruit(spy)
  raise AlreadyStubbedError if @spies[spy.object_id]
  check_spy!(spy)
  @spies[spy.object_id] = spy
end
retire(spy) click to toggle source

remove spy from the records @param spy [Subroutine, Constant, Double] @return [spy]

# File lib/spy/agency.rb, line 32
def retire(spy)
  raise NoSpyError unless @spies[spy.object_id]
  @spies.delete(spy.object_id)
end
spies() click to toggle source

returns all the spies that have been initialized since the creation of this agency @return [Array<Subroutine, Constant, Double>]

# File lib/spy/agency.rb, line 64
def spies
  @spies.values
end

Private Instance Methods

check_spy!(spy) click to toggle source
# File lib/spy/agency.rb, line 70
def check_spy!(spy)
  raise ArgumentError, "#{spy}, was not a spy" unless spy.is_a?(Base)
end