class RSpectre::Linter::UnusedSharedSetup

Constants

TAG

Public Class Methods

redefine_shared(receiver, method) click to toggle source
# 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, &block|
    # When we can locate the source of the node, tag it
    if (node = UnusedSharedSetup.register(method, caller_locations))
      # And call the orignal
      original_method.(name, *args) do |*shared_args|
        # 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, &block)
      end
    else
      # If we couldn't locate the source, just delegate to the original method.
      original_method.(name, *args, &block)
    end
  end
end