module TrackOpenInstances

Mixin to track instances of a class that require explicit cleanup

This module provides a mechanism to track instances of classes that need to be explicitly closed or cleaned up. It maintains a list of all instances created and allows checking for any unclosed instances, which can help in identifying resource leaks.

@example Basic Usage

class ManagedFile
  include TrackOpenInstances

  attr_reader :path

  def initialize(path)
    @path = path
    # Simulate opening the file
    puts "Opening file: #{path}"
    # Register the instance for tracking
    self.class.add_open_instance(self)
  end

  # Implement the close logic specific to the resource
  def close
    # Simulate closing the file
    puts "Closing file: #{path}"
    # Remove the instance from tracking
    self.class.remove_open_instance(self)
  end
end

file1 = ManagedFile.new('/tmp/file1.txt')
file2 = ManagedFile.new('/tmp/file2.txt')

puts ManagedFile.open_instances.count #=> 2
puts ManagedFile.open_instances.keys.inspect #=> [#<ManagedFile:...>, #<ManagedFile:...>]

file1.close

puts ManagedFile.open_instances.count #=> 1
puts ManagedFile.open_instances.keys.inspect #=> [#<ManagedFile:...>]

# In a test suite's teardown, you might use:
# ManagedFile.assert_no_open_instances # This would raise if file2 wasn't closed

file2.close
ManagedFile.assert_no_open_instances # This will now pass

@api public