Module: TrackOpenInstances

Defined in:
lib/track_open_instances.rb,
lib/track_open_instances/version.rb

Overview

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.

Examples:

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

Defined Under Namespace

Modules: ClassMethods Classes: OpenInstance

Constant Summary collapse

VERSION =

The last released version of this gem

'0.1.11'

Class Method Summary collapse

Class Method Details

.included(base)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Ruby hook executed when this module is included in a base class

Sets up the necessary class instance variables and extends the base class with ClassMethods.

Parameters:

  • base (Class)

    The class including this module



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/track_open_instances.rb', line 91

def self.included(base)
  base.extend(ClassMethods)
  # @!attribute [rw] open_instances
  #   Internal storage for all tracked instances
  #   @return [Hash{Object => OpenInstance}] The list of currently tracked instances
  base.instance_variable_set(:@open_instances, {}.compare_by_identity)
  # @!visibility private
  # @!attribute [r] open_instances_mutex
  #   Mutex to ensure thread-safe access to the open_instances hash
  base.instance_variable_set(:@open_instances_mutex, Thread::Mutex.new)
end