Module: ActiveSupport::DescendantsTracker

Defined in:
lib/active_support/descendants_tracker.rb

Overview

Active Support Descendants Tracker

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 ActiveSupport::DescendantsTracker does not provide any benefit.

Defined Under Namespace

Modules: ReloadedClassesFiltering Classes: WeakSet

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear(classes) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
86
87
# File 'lib/active_support/descendants_tracker.rb', line 78

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) ⇒ Object



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

def descendants(klass)
  klass.descendants
end

.disable_clear!Object

:nodoc:



69
70
71
72
73
74
75
76
# File 'lib/active_support/descendants_tracker.rb', line 69

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) ⇒ Object

:nodoc:



89
90
91
92
93
94
# File 'lib/active_support/descendants_tracker.rb', line 89

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

.subclasses(klass) ⇒ Object



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

def subclasses(klass)
  klass.subclasses
end

Instance Method Details

#descendantsObject



107
108
109
110
# File 'lib/active_support/descendants_tracker.rb', line 107

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