aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/descendants_tracker.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-06-19 16:44:35 +0200
committerJosé Valim <jose.valim@gmail.com>2010-06-19 16:44:35 +0200
commit8db8c6f4ce3e8dd7f90553ab7866bf9991773b98 (patch)
tree38b7b95a5215266ec0836d4c4b0cc097b10280e5 /activesupport/lib/active_support/descendants_tracker.rb
parenta186431414de8a0f0db9f60254f421a3536cee12 (diff)
downloadrails-8db8c6f4ce3e8dd7f90553ab7866bf9991773b98.tar.gz
rails-8db8c6f4ce3e8dd7f90553ab7866bf9991773b98.tar.bz2
rails-8db8c6f4ce3e8dd7f90553ab7866bf9991773b98.zip
Add ActiveSupport::DescendantsTracker.
Diffstat (limited to 'activesupport/lib/active_support/descendants_tracker.rb')
-rw-r--r--activesupport/lib/active_support/descendants_tracker.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/descendants_tracker.rb b/activesupport/lib/active_support/descendants_tracker.rb
new file mode 100644
index 0000000000..a9379bf95b
--- /dev/null
+++ b/activesupport/lib/active_support/descendants_tracker.rb
@@ -0,0 +1,36 @@
+require 'active_support/dependencies'
+
+module ActiveSupport
+ # This module provides an internal implementation to track descendants
+ # which is faster than iterating through ObjectSpace.
+ module DescendantsTracker
+ mattr_accessor :descendants
+ @@descendants = Hash.new { |h, k| h[k] = [] }
+
+ def self.clear
+ @@descendants.each do |klass, descendants|
+ if ActiveSupport::Dependencies.autoloaded?(klass)
+ @@descendants.delete(klass)
+ else
+ descendants.reject! { |v| ActiveSupport::Dependencies.autoloaded?(v) }
+ end
+ end
+ end
+
+ def inherited(base)
+ self.direct_descendants << base
+ super
+ end
+
+ def direct_descendants
+ @@descendants[self]
+ end
+
+ def descendants
+ @@descendants[self].inject([]) do |descendants, klass|
+ descendants << klass
+ descendants.concat klass.descendants
+ end
+ end
+ end
+end \ No newline at end of file