Module: Spy::API

Included in:
Spy
Defined in:
lib/spy/api.rb

Instance Method Summary collapse

Instance Method Details

#on(target, msg) ⇒ Object

Spies on calls to a method made on an object

Parameters:

  • target
    • the object you want to spy on

  • msg
    • the name of the method to spy on



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

def on(target, msg)
  core.add_spy(target, target.method(msg))
end

#on_any_instance(target, msg) ⇒ Object

Spies on calls to a method made on any instance of some class or module

Parameters:

  • target
    • class or module to spy on

  • msg
    • name of the method to spy on

Raises:

  • (ArgumentError)


18
19
20
21
# File 'lib/spy/api.rb', line 18

def on_any_instance(target, msg)
  raise ArgumentError unless target.respond_to?(:instance_method)
  core.add_spy(target, target.instance_method(msg))
end

#restore(*args) ⇒ Object

Stops spying on the method and restores its original functionality

Parameters:

  • args
    • supports multiple signatures

    Spy.restore(:all)

    => stops spying on every spied message
    

    Spy.restore(receiver, msg)

    => stops spying on the given receiver and message (assumes :method)
    

    Spy.restore(reciever, msg, method_type)

    =>  stops spying on the given receiver and message of method_type
    


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spy/api.rb', line 35

def restore(*args)
  case args.length
  when 1
    core.remove_all_spies if args.first == :all
  when 2
    target, msg = *args
    core.remove_spy(target, target.method(msg))
  when 3
    target, msg, method_type = *args
    core.remove_spy(target, target.send(method_type, msg))
  else
    raise ArgumentError
  end
end