lib/mixins/family_tree.rb in bblib-0.4.1 vs lib/mixins/family_tree.rb in bblib-1.0.2
- old
+ new
@@ -4,19 +4,21 @@
# Various methods for finding descendants and subclasses of a class. Intended as an
# extend mixin for any class.
module FamilyTree
# Return all classes that inherit from this class
def descendants(include_singletons = false)
+ return _inherited_by.map { |c| [c, c.descendants] }.flatten.uniq if BBLib.in_opal?
ObjectSpace.each_object(Class).select do |c|
(include_singletons || !c.singleton_class?) && c < self
end
end
alias subclasses descendants
# Return all classes that directly inherit from this class
def direct_descendants(include_singletons = false)
+ return _inherited_by if BBLib.in_opal?
ObjectSpace.each_object(Class).select do |c|
(include_singletons || !c.singleton_class?) && c.ancestors[1] == self
end
end
@@ -35,7 +37,17 @@
BBLib.root_namespace_of(self)
end
alias direct_subclasses direct_descendants
+ # If we are in Opal we need to track descendants a bit differently
+ if BBLib.in_opal?
+ def _inherited_by
+ @_inherited_by ||= []
+ end
+
+ def inherited(klass)
+ _inherited_by.push(klass)
+ end
+ end
end
end