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

Instance Method Details

#include?(spied, method) ⇒ Boolean

Returns whether or not the object and method are already being spied on

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/spy/registry.rb', line 45

def include?(spied, method)
  entry = RegistryEntry.new(spied, method)
  store.include? entry
end

#insert(spied, method, spy) ⇒ Object

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

Parameters:

  • spied (Object)
    • the object being spied on

  • method (Method, UnboundMethod)
    • the method being spied on

  • spy (Spy::Instance)
    • the instantiated spy

Raises:



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

def insert(spied, method, spy)
  entry = RegistryEntry.new(spied, method, spy)
  raise Errors::AlreadySpiedError if store.include? entry
  store.insert(entry)
end

#remove(spied, method) ⇒ Object

Stops tracking the spy

Parameters:

  • spied (Object)
    • the object being spied on

  • method (Method, UnboundMethod)
    • the method being spied on

Raises:



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

def remove(spied, method)
  entry = RegistryEntry.new(spied, method, nil)
  raise Errors::MethodNotSpiedError unless store.include? entry
  store.remove(entry).spy
end

#remove_allObject

Stops tracking all spies



36
37
38
39
# File 'lib/spy/registry.rb', line 36

def remove_all
  store.map { |e| yield remove(e.spied, e.method) }
  raise Errors::UnableToEmptySpyRegistryError unless store.empty?
end