Module: ActiveSupport::DescendantsTracker
  
  
  
  
  
  
  
  
  
  
  
  
    - Defined in:
 
    - lib/active_support/descendants_tracker.rb
 
  
  
 
Overview
  
    
This module provides an internal implementation to track descendants which is faster than iterating through ObjectSpace.
   
 
  
Defined Under Namespace
  
    
  
    
      Classes: DescendantsArray
    
  
  
    
      Constant Summary
      collapse
    
    
      
        - @@excluded_descendants =
          
        
 
        if RUBY_ENGINE == "ruby"
      ObjectSpace::WeakMap.new
else
            class WeakSet     def initialize
      @map = ObjectSpace::WeakMap.new
    end
     def [](object)
      @map.key?(object.object_id)
    end
     def []=(object, _present)
      @map[object.object_id] = object
    end
  end
  WeakSet.new
end 
      
        - @@direct_descendants =
          
        
 
        {} 
      
    
  
  
    
      Class Method Summary
      collapse
    
    
  
    
      Instance Method Summary
      collapse
    
    
  
  
    Class Method Details
    
      
  
  
    .clear(classes)  ⇒ Object 
  
  
  
  
  
    
      
66
67
68
69
70
71
72
73
74
75 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 66
def clear(classes)   raise "DescendantsTracker.clear was disabled because config.cache_classes = true" if @clear_disabled
  classes.each do |klass|
    @@excluded_descendants[klass] = true
    klass.descendants.each do |descendant|
      @@excluded_descendants[descendant] = true
    end
  end
end
     | 
  
 
    
      
  
  
    .descendants(klass)  ⇒ Object 
  
  
  
  
    
      
62
63
64 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 62
def descendants(klass)
  klass.descendants
end 
     | 
  
 
    
      
  
  
    .direct_descendants(klass)  ⇒ Object 
  
  
  
  
    
      
11
12
13
14
15
16
17 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 11
def direct_descendants(klass)
  ActiveSupport::Deprecation.warn(<<~MSG)
    ActiveSupport::DescendantsTracker.direct_descendants is deprecated and will be removed in Rails 7.1.
    Use ActiveSupport::DescendantsTracker.subclasses instead.
  MSG
  subclasses(klass)
end
     | 
  
 
    
      
  
  
    .disable_clear!  ⇒ Object 
  
  
  
  
  
    
      
50
51
52
53
54
55
56 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 50
def disable_clear!   unless @clear_disabled
    @clear_disabled = true
    remove_method(:subclasses)
    @@excluded_descendants = nil
  end
end
     | 
  
 
    
      
  
  
    .native?  ⇒ Boolean 
  
  
  
  
    
      
77
78
79 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 77
def native?   true
end 
     | 
  
 
    
      
  
  
    .store_inherited(klass, descendant)  ⇒ Object 
  
  
  
  
    
This is the only method that is not thread safe, but is only ever called during the eager loading phase.
   
 
  
  
    
      
138
139
140 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 138
def store_inherited(klass, descendant)
  (@@direct_descendants[klass] ||= DescendantsArray.new) << descendant
end 
     | 
  
 
    
      
  
  
    .subclasses(klass)  ⇒ Object 
  
  
  
  
    
      
58
59
60 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 58
def subclasses(klass)
  klass.subclasses
end 
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    #descendants  ⇒ Object 
  
  
  
  
    
      
88
89
90 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 88
def descendants
  subclasses.concat(subclasses.flat_map(&:descendants))
end 
     | 
  
 
    
      
  
  
    #direct_descendants  ⇒ Object 
  
  
  
  
    
      
92
93
94
95
96
97
98 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 92
def direct_descendants
  ActiveSupport::Deprecation.warn(<<~MSG)
    ActiveSupport::DescendantsTracker#direct_descendants is deprecated and will be removed in Rails 7.1.
    Use #subclasses instead.
  MSG
  subclasses
end
     | 
  
 
    
      
    
      
  
  
    #subclasses  ⇒ Object 
  
  
  
  
    
      
82
83
84
85
86 
     | 
    
      # File 'lib/active_support/descendants_tracker.rb', line 82
def subclasses
  subclasses = super
  subclasses.reject! { |d| @@excluded_descendants[d] }
  subclasses
end
     |