123456789_123456789_123456789_123456789_123456789_

Module: ActiveSupport::DescendantsTracker

Overview

==================================

This module provides an internal implementation to track descendants which is faster than iterating through ObjectSpace.

However Ruby 3.1 provide a fast native Class#subclasses method, so if you know your code won't be executed on older rubies, including DescendantsTracker does not provide any benefit.

Class Method Summary

Instance Method Summary

Class Method Details

.clear(classes)

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 80

def clear(classes) # :nodoc:
  raise "DescendantsTracker.clear was disabled because config.enable_reloading is false" if @clear_disabled

  classes.each do |klass|
    @excluded_descendants << klass
    klass.descendants.each do |descendant|
      @excluded_descendants << descendant
    end
  end
end

.descendants(klass)

[ GitHub ]

  
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 104

def descendants(klass)
  klass.descendants
end

.disable_clear!

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 71

def disable_clear! # :nodoc:
  unless @clear_disabled
    @clear_disabled = true
    ReloadedClassesFiltering.remove_method(:subclasses)
    ReloadedClassesFiltering.remove_method(:descendants)
    @excluded_descendants = nil
  end
end

.reject!(classes)

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 91

def reject!(classes) # :nodoc:
  if @excluded_descendants
    classes.reject! { |d| @excluded_descendants.include?(d) }
  end
  classes
end

.subclasses(klass)

[ GitHub ]

  
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 100

def subclasses(klass)
  klass.subclasses
end

Instance Method Details

#descendants

[ GitHub ]

  
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 109

def descendants
  subclasses = DescendantsTracker.reject!(self.subclasses)
  subclasses.concat(subclasses.flat_map(&:descendants))
end