class Unused::Registry

Public Class Methods

class_map() click to toggle source
# File lib/unused/registry.rb, line 34
def self.class_map
  instance.class_map
end
class_method_calls() click to toggle source
# File lib/unused/registry.rb, line 26
def self.class_method_calls
  instance.class_method_calls
end
instance_method_calls() click to toggle source
# File lib/unused/registry.rb, line 30
def self.instance_method_calls
  instance.instance_method_calls
end
log_class_method(callee_id, method_id) click to toggle source
# File lib/unused/registry.rb, line 18
def self.log_class_method(callee_id, method_id)
  instance.log_class_method(callee_id, method_id)
end
log_instance_method(callee_id, method_id) click to toggle source
# File lib/unused/registry.rb, line 14
def self.log_instance_method(callee_id, method_id)
  instance.log_instance_method(callee_id, method_id)
end
new() click to toggle source
# File lib/unused/registry.rb, line 38
def initialize
  @class_method_calls = {}
  @instance_method_calls = {}
  @class_id_map = {}
end
register(defined_class) click to toggle source

delegate class method calls to instance

# File lib/unused/registry.rb, line 10
def self.register(defined_class)
  instance.register(defined_class)
end
tracked_objects() click to toggle source
# File lib/unused/registry.rb, line 22
def self.tracked_objects
  instance.tracked_objects
end

Public Instance Methods

class_map() click to toggle source
# File lib/unused/registry.rb, line 107
def class_map
  # getter returns clone so as not to allow mutation
  @class_id_map.clone
end
class_method_calls() click to toggle source
# File lib/unused/registry.rb, line 102
def class_method_calls
  # getter returns clone so as not to allow mutation
  @class_method_calls.clone
end
instance_method_calls() click to toggle source
# File lib/unused/registry.rb, line 97
def instance_method_calls
  # getter returns clone so as not to allow mutation
  @instance_method_calls.clone
end
log_class_method(callee_id, method) click to toggle source
# File lib/unused/registry.rb, line 77
def log_class_method(callee_id, method)
  hash_key = [callee_id, method]

  # metaprogramming can sometime define methods not in the registry
  # skip in this case
  return unless @class_method_calls.key?(hash_key)

  incrment_class_method_call(hash_key)
end
log_instance_method(callee_class_id, method) click to toggle source
# File lib/unused/registry.rb, line 87
def log_instance_method(callee_class_id, method)
  hash_key = [callee_class_id, method]

  # metaprogramming can sometime define methods not in the registry
  # skip in this case
  return unless @instance_method_calls.key?(hash_key)

  increment_instance_method_call(hash_key)
end
register(defined_class) click to toggle source
# File lib/unused/registry.rb, line 54
def register(defined_class)
  id = defined_class._object_id_UNUSED_ALIAS_
  @class_id_map[id] = defined_class
  instance_methods = defined_class._instance_methods_UNUSED_ALIAS_(false) +
                     defined_class._private_instance_methods_UNUSED_ALIAS_(false)

  instance_methods.each do |method|
    key = [id, method]
    next if @instance_method_calls.key?(key)
    next if method.to_s.include? '_UNUSED_ALIAS_'

    source, = defined_class._instance_method_UNUSED_ALIAS_(method).source_location
    unless source &&
           File.expand_path(source).start_with?(Unused.config.path)
      next
    end

    @instance_method_calls.store(key, 0)
  end

  register_class_methods(defined_class)
end
reset() click to toggle source
# File lib/unused/registry.rb, line 44
def reset
  @class_method_calls = {}
  @instance_method_calls = {}
  @class_id_map = {}
end
tracked_objects() click to toggle source
# File lib/unused/registry.rb, line 50
def tracked_objects
  @class_id_map.keys
end

Private Instance Methods

increment_instance_method_call(hash_key) click to toggle source
# File lib/unused/registry.rb, line 144
def increment_instance_method_call(hash_key)
  @instance_method_calls[hash_key] += 1
end
incrment_class_method_call(hash_key) click to toggle source
# File lib/unused/registry.rb, line 148
def incrment_class_method_call(hash_key)
  @class_method_calls[hash_key] += 1
end
register_class_methods(defined_class) click to toggle source
# File lib/unused/registry.rb, line 114
def register_class_methods(defined_class)
  # Class methods are owned by a defined class's "singleton"
  # Singleton classes are only created on class method definition
  # or when calling Class#singleton_class
  # Thus, to prevent a needless allocation, check if there is
  # any need to register the singleton

  # :inherited and :initialize defined on all classes
  class_methods = defined_class._private_methods_UNUSED_ALIAS_(false) -
                  %i[inherited initialize]
  class_methods += defined_class._methods_UNUSED_ALIAS_(false)
  return if class_methods.empty?

  singleton_id = defined_class._singleton_class_UNUSED_ALIAS_.object_id
  @class_id_map[singleton_id] = defined_class
  class_methods.each do |method|
    key = [singleton_id, method]
    next if @class_method_calls.key?(key)
    next if method.to_s.include? '_UNUSED_ALIAS_'

    source, = defined_class._method_UNUSED_ALIAS_(method).source_location
    unless source &&
           File.expand_path(source).start_with?(Unused.config.path)
      next
    end

    @class_method_calls.store(key, 0)
  end
end