Class: Spy::Core

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

Overview

The main internal API. This is used directly by ‘Spy::API` and is the primary control center for creating and removing spies.

Syntactic sugar (like ‘Spy.restore(object, msg)` vs `Spy.restore(:all)`) should be handled in `Spy::API` and utilize `Spy::Core`

Instance Method Summary collapse

Instance Method Details

#add_spy(object, method) ⇒ Object

Start spying on the given object and method

Parameters:

  • object (Object)
    • the object to spy on

  • method (Method, UnboundMethod)
    • the method to spy on

Raises:



19
20
21
22
23
24
# File 'lib/spy/core.rb', line 19

def add_spy(object, method)
  raise Errors::AlreadySpiedError if registry.include?(object, method)
  spy = Instance.new(object, method)
  registry.insert(object, method, spy)
  spy.start
end

#remove_all_spiesObject

Stops spying on all objects and methods



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

def remove_all_spies
  registry.remove_all(&:stop)
end

#remove_spy(object, method) ⇒ Object

Stop spying on the given object and method

Parameters:

  • object (Object)
    • the object being spied on

  • method (Method, UnboundMethod)
    • the method to stop spying on



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

def remove_spy(object, method)
  spy = registry.remove(object, method)
  spy.stop
end