Module: Spy::ReplaceMethod

Defined in:
lib/spy/replace_method.rb

Class Method Summary collapse

Class Method Details

.call(klass, spy, mode: nil, remove_existing: false) ⇒ Object



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

def self.call(klass, spy, mode: nil, remove_existing: false)
  klass.class_eval do
    name = spy.original.name

    remove_method(name) if remove_existing

    case mode
    when :stub
      define_method(name, ReplaceMethod.impl(spy))
    when :restore
      define_method(name, spy.original)
    end
  end
end

.impl(spy) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/spy/replace_method.rb', line 20

def self.impl(spy)
  proc do |*args, &block|
    backtrace = caller.drop_while { |path| path =~ /lib\/spy\/replace_method\.rb$/ }
    method_call = MethodCall.new(spy, self, args, block, backtrace)
    spy.apply(method_call)
  end
end