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.

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject


20
21
22
23
24
25
26
27
28
# File '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

.descendants(klass) ⇒ Object


13
14
15
16
17
18
# File '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

.direct_descendants(klass) ⇒ Object


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

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

Instance Method Details

#descendantsObject


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

def descendants
  DescendantsTracker.descendants(self)
end

#direct_descendantsObject


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

def direct_descendants
  DescendantsTracker.direct_descendants(self)
end

#inherited(base) ⇒ Object


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

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