Class: RSpectre::Linter::UnusedSharedSetup

Inherits:
RSpectre::Linter show all
Defined in:
lib/rspectre/linter/unused_shared_setup.rb

Constant Summary collapse

TAG =
'UnusedSharedSetup'

Constants inherited from RSpectre::Linter

SOURCE_FILES

Class Method Summary collapse

Methods inherited from RSpectre::Linter

example_group, node_map, prepend_behavior, record, register

Class Method Details

.redefine_shared(receiver, method) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rspectre/linter/unused_shared_setup.rb', line 8

def self.redefine_shared(receiver, method)
  # Capture the original class method
  original_method = receiver.method(method)

  # Overwrite the class method using define_singleton_method
  receiver.__send__(:define_singleton_method, method) do |name, *args, **kwargs, &block|
    # When we can locate the source of the node, tag it
    if (node = UnusedSharedSetup.register(method, caller_locations))
      # And call the original
      original_method.(name, *args, **kwargs) do |*shared_args, **shared_kwargs|
        # But record that it was used in a `before`
        before { UnusedSharedSetup.record(node) }

        # And then perform the original block in a `class_exec` like the original block was
        # supposed to be
        class_exec(*shared_args, **shared_kwargs, &block)
      end
    else
      # If we couldn't locate the source, just delegate to the original method.
      original_method.(name, *args, **kwargs, &block)
    end
  end
end