Module: Spy::API

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

Instance Method Summary collapse

Instance Method Details

#on(*args) ⇒ Object

Initializes a new spy instance for the method

With two args:

Parameters:

  • receiver
    • the receiver of the message you want to spy on

  • msg
    • the message passed to the receiver that you want to spy on

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
# File 'lib/spy/api.rb', line 10

def on(*args)
  case args.length
  when 2
    spied, msg = *args
    return core.add_spy(spied, spied.method(msg))
  end
  raise ArgumentError
end

#on_any_instance(spied, msg) ⇒ Object

TODO docs



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

def on_any_instance(spied, msg)
  core.add_spy(spied, spied.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
    

Raises:

  • (ArgumentError)


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

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