class DeadCodeDetector::ClassMethodWrapper

Public Class Methods

record_key(class_name) click to toggle source
# File lib/dead_code_detector/class_method_wrapper.rb, line 14
def record_key(class_name)
  "dead_code_detector/record_keeper/#{class_name}/class_methods"
end
unwrap_method(klass, original_method) click to toggle source
# File lib/dead_code_detector/class_method_wrapper.rb, line 5
def unwrap_method(klass, original_method)
  if klass.singleton_class == original_method.owner
    klass.define_singleton_method(original_method.name, original_method)
  else
    klass.singleton_class.send(:remove_method, original_method.name)
  end
  track_method(klass, original_method.name)
end

Public Instance Methods

get_method(method_name) click to toggle source
# File lib/dead_code_detector/class_method_wrapper.rb, line 19
def get_method(method_name)
  klass.method(method_name)
end

Private Instance Methods

default_methods() click to toggle source
# File lib/dead_code_detector/class_method_wrapper.rb, line 46
def default_methods
  @default_methods ||= klass.methods.map(&:to_s).select do |method_name|
    owned_method?(method_name) && target_directory?(method_name)
  end
end
owned_method?(method_name) click to toggle source
# File lib/dead_code_detector/class_method_wrapper.rb, line 60
def owned_method?(method_name)
  original_method = klass.method(method_name)
  if klass.respond_to?(:superclass)
    klass.singleton_class <= original_method.owner && !(klass.superclass.singleton_class <= original_method.owner)
  else
    klass.singleton_class <= original_method.owner
  end
end
target_directory?(method_name) click to toggle source
# File lib/dead_code_detector/class_method_wrapper.rb, line 52
def target_directory?(method_name)
  return true if DeadCodeDetector.config.ignore_paths.nil?
  source_location = klass.method(method_name).source_location&.first
  return false if source_location.nil?
  return false if source_location == "(eval)"
  source_location !~ DeadCodeDetector.config.ignore_paths
end
wrap_method(original_method) click to toggle source
# File lib/dead_code_detector/class_method_wrapper.rb, line 25
def wrap_method(original_method)
  original_class = klass
  klass.define_singleton_method(original_method.name) do |*args, &block|
    begin
      DeadCodeDetector::ClassMethodWrapper.unwrap_method(original_class, original_method)
    rescue StandardError => e
      if DeadCodeDetector.config.error_handler
        DeadCodeDetector.config.error_handler.call(e)
      end
    end
    # We may have a method like `ActiveRecord::Base.sti_name`
    # that begins bound to `ActiveRecord::Base`
    # However, it may be called from `User.sti_name`
    # We need to bind the original method to the class that
    # is calling the method
    unbound_method = original_method.unbind
    method_bound_to_caller = unbound_method.bind(self)
    method_bound_to_caller.call(*args, &block)
  end
end