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 79

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)

See additional method definition at line 104.

[ GitHub ]

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

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 70

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 90

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

.store_inherited(klass, descendant)

This method is for internal use only.

This is the only method that is not thread safe, but is only ever called during the eager loading phase.

[ GitHub ]

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

def store_inherited(klass, descendant) # :nodoc:
  (@direct_descendants[klass] ||= DescendantsArray.new) << descendant
end

.subclasses(klass)

See additional method definition at line 100.

[ GitHub ]

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

def subclasses(klass)
  klass.subclasses
end

Instance Method Details

#descendants

See additional method definition at line 109.

[ GitHub ]

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

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

#inherited(base) (private)

This method is for internal use only.
[ GitHub ]

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

def inherited(base) # :nodoc:
  DescendantsTracker.store_inherited(self, base)
  super
end

#subclasses

[ GitHub ]

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

def subclasses
  DescendantsTracker.subclasses(self)
end