Class: Spy::Registry

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

Overview

Responsible for managing the top-level state of which spies exist.

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



6
7
8
# File 'lib/spy/registry.rb', line 6

def initialize
  @store = {}
end

Instance Method Details

#get(blueprint) ⇒ Object



39
40
41
42
# File 'lib/spy/registry.rb', line 39

def get(blueprint)
  key = blueprint.to_s
  @store[key]
end

#insert(blueprint, spy) ⇒ Object

Keeps track of the spy for later management. Ensures spy uniqueness

Parameters:

Raises:



16
17
18
19
20
# File 'lib/spy/registry.rb', line 16

def insert(blueprint, spy)
  key = blueprint.to_s
  raise Errors::AlreadySpiedError if @store[key]
  @store[key] = [blueprint, spy]
end

#remove(blueprint) ⇒ Object

Stops tracking the spy



26
27
28
29
30
# File 'lib/spy/registry.rb', line 26

def remove(blueprint)
  key = blueprint.to_s
  raise Errors::MethodNotSpiedError unless @store[key]
  @store.delete(key)[1]
end

#remove_allObject

Stops tracking all spies



33
34
35
36
37
# File 'lib/spy/registry.rb', line 33

def remove_all
  store = @store
  @store = {}
  store.values.map(&:last)
end