Module: ActiveSupport::DescendantsTracker

Defined in:
activesupport/lib/active_support/descendants_tracker.rb

Overview

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

Constant Summary

@@direct_descendants =
Hash.new { |h, k| h[k] = [] }

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) clear



20
21
22
23
24
25
26
27
28
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 20

def self.clear
  @@direct_descendants.each do |klass, descendants|
    if ActiveSupport::Dependencies.autoloaded?(klass)
      @@direct_descendants.delete(klass)
    else
      descendants.reject! { |v| ActiveSupport::Dependencies.autoloaded?(v) }
    end
  end
end

+ (Object) descendants(klass)



13
14
15
16
17
18
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 13

def self.descendants(klass)
  @@direct_descendants[klass].inject([]) do |descendants, _klass|
    descendants << _klass
    descendants.concat _klass.descendants
  end
end

+ (Object) direct_descendants(klass)



9
10
11
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 9

def self.direct_descendants(klass)
  @@direct_descendants[klass]
end

Instance Method Details

- (Object) descendants



39
40
41
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 39

def descendants
  DescendantsTracker.descendants(self)
end

- (Object) direct_descendants



35
36
37
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 35

def direct_descendants
  DescendantsTracker.direct_descendants(self)
end

- (Object) inherited(base)



30
31
32
33
# File 'activesupport/lib/active_support/descendants_tracker.rb', line 30

def inherited(base)
  self.direct_descendants << base
  super
end