Class: MockSuey::Tracer::PrependCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/mock_suey/tracer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracer) ⇒ PrependCollector

Returns a new instance of PrependCollector.



8
9
10
# File 'lib/mock_suey/tracer.rb', line 8

def initialize(tracer)
  @tracer = tracer
end

Instance Attribute Details

#tracerObject (readonly)

Returns the value of attribute tracer.



6
7
8
# File 'lib/mock_suey/tracer.rb', line 6

def tracer
  @tracer
end

Instance Method Details

#module_for(klass, methods) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mock_suey/tracer.rb', line 12

def module_for(klass, methods)
  tracer = self.tracer

  Module.new do
    define_method(:__mock_suey_tracer__) { tracer }

    methods.each do |mid|
      module_eval <<~RUBY, __FILE__, __LINE__ + 1
        def #{mid}(*args, **kwargs, &block)
          super.tap do |return_value|
            arguments = args
            arguments << kwargs unless kwargs.empty?

            __mock_suey_tracer__ << MethodCall.new(
              receiver_class: self.class,
              method_name: __method__,
              arguments: arguments,
              has_kwargs: !kwargs.empty?,
              return_value: return_value,
              metadata: {
                location: method(__method__).super_method.source_location.first
              }
            )
          end
        end
      RUBY
    end
  end
end

#setup(targets) ⇒ Object



42
43
44
45
46
47
# File 'lib/mock_suey/tracer.rb', line 42

def setup(targets)
  targets.each do |klass, methods|
    mod = module_for(klass, methods)
    klass.prepend(mod)
  end
end

#stopObject



49
50
# File 'lib/mock_suey/tracer.rb', line 49

def stop
end