Class: Spy::Agency

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/spy/agency.rb

Overview

Manages all the spies

Instance Method Summary collapse

Constructor Details

#initializeAgency

Returns a new instance of Agency.



9
10
11
# File 'lib/spy/agency.rb', line 9

def initialize
  clear!
end

Instance Method Details

#active?(spy) ⇒ Boolean

checks to see if a spy is hooked

Parameters:

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/spy/agency.rb', line 40

def active?(spy)
  check_spy!(spy)
  @spies.has_key?(spy.object_id)
end

#clear!self

clears records

Returns:

  • (self)


56
57
58
59
# File 'lib/spy/agency.rb', line 56

def clear!
  @spies = {}
  self
end

#dissolve!self

unhooks all spies and clears records

Returns:

  • (self)


47
48
49
50
51
52
# 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) ⇒ Nil, ...

given a spy ID it will return the associated spy

Parameters:

  • id (Integer)

    spy object id

Returns:



16
17
18
# File 'lib/spy/agency.rb', line 16

def find(id)
  @spies[id]
end

#recruit(spy) ⇒ spy

Record that a spy was initialized and hooked

Parameters:

Returns:

  • (spy)

Raises:



23
24
25
26
27
# 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) ⇒ spy

remove spy from the records

Parameters:

Returns:

  • (spy)

Raises:



32
33
34
35
# File 'lib/spy/agency.rb', line 32

def retire(spy)
  raise NoSpyError unless @spies[spy.object_id]
  @spies.delete(spy.object_id)
end

#spiesArray<Subroutine, Constant, Double>

returns all the spies that have been initialized since the creation of this agency

Returns:



64
65
66
# File 'lib/spy/agency.rb', line 64

def spies
  @spies.values
end