Class: Spy::Strategy::Intercept

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

Instance Method Summary collapse

Constructor Details

#initialize(spy) ⇒ Intercept

Returns a new instance of Intercept.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/spy/strategy/intercept.rb', line 7

def initialize(spy)
  @spy = spy
  @target =
    case spy.original
    when Method
      spy.spied.singleton_class
    when UnboundMethod
      spy.spied
    when FakeMethod
      spy.spied.singleton_class
    end
end

Instance Method Details

#applyObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spy/strategy/intercept.rb', line 20

def apply
  spy = @spy
  @target.class_eval do
    # Add the spy to the intercept target
    define_method spy.original.name do |*args, &block|
      Spy::Strategy::Base.call(spy, self, *args, &block)
    end

    # Make the visibility of the spy match the spied original
    unless spy.original.is_a?(FakeMethod)
      visibility = DetermineVisibility.call(spy.original)
      send(visibility, spy.original.name)
    end
  end
end

#undoObject



36
37
38
39
40
41
# File 'lib/spy/strategy/intercept.rb', line 36

def undo
  spy = @spy
  @target.class_eval do
    remove_method spy.original.name
  end
end