class RBS::AncestorGraph
AncestorGraph is a utility class that helps iterating through ancestors and descendants of a class/module
graph = AncestorGraph.new(env: env, ancestor_builder: ancestor_builder) graph.each_parent(AncestorGraph::InstanceNode.new(type_name: TypeName.parse("::Object"))) graph.each_ancestor(AncestorGraph::InstanceNode.new(type_name: TypeName.parse("::Object"))) graph.each_child(AncestorGraph::InstanceNode.new(type_name: TypeName.parse("::Object"))) graph.each_descendant(AncestorGraph::InstanceNode.new(type_name: TypeName.parse("::Object")))
Note that the class works for class/module declarations. All of the alias classes/modules are ignored.
-
Alias classes/modules doesn’t count as a parent nor child
-
Passing alias classes/modules to the method doesn’t yield anything
Constants
- InstanceNode
- SingletonNode
Attributes
ancestor_builder
[R]
DefinitionBuilder::AncestorBuilder
children
[R]
Hash[node, Set[node]]
parents
[R]
Hash[node, Set[node]]
Public Class Methods
(env: Environment, ?ancestor_builder: DefinitionBuilder::AncestorBuilder) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ancestor_graph.rb, line 13 def initialize(env:, ancestor_builder: DefinitionBuilder::AncestorBuilder.new(env: env)) @env = env @ancestor_builder = ancestor_builder build() end
Public Instance Methods
() → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ancestor_graph.rb, line 19 def build() @parents = {} @children = {} env.class_decls.each_key do |type_name| build_ancestors(InstanceNode.new(type_name: type_name), ancestor_builder.one_instance_ancestors(type_name)) build_ancestors(SingletonNode.new(type_name: type_name), ancestor_builder.one_singleton_ancestors(type_name)) end env.interface_decls.each_key do |type_name| build_ancestors(InstanceNode.new(type_name: type_name), ancestor_builder.one_interface_ancestors(type_name)) end end
(node, DefinitionBuilder::AncestorBuilder::OneAncestors) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ancestor_graph.rb, line 32 def build_ancestors(node, ancestors) ancestors.each_ancestor do |ancestor| case ancestor when Definition::Ancestor::Instance register(child: node, parent: InstanceNode.new(type_name: ancestor.name)) when Definition::Ancestor::Singleton register(child: node, parent: SingletonNode.new(type_name: ancestor.name)) end end end
(node, ?yielded: Set[node]) { (node) → void } → void
(node) → Enumerator[node, void]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ancestor_graph.rb, line 64 def each_ancestor(node, yielded: Set[], &block) if block each_parent(node) do |parent| unless yielded.member?(parent) yielded << parent yield parent each_ancestor(parent, yielded: yielded, &block) end end else enum_for :each_ancestor, node end end
(node) { (node) → void } → void
(node) → Enumerator[node, void]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ancestor_graph.rb, line 56 def each_child(node, &block) if block children[node]&.each(&block) else enum_for :each_child, node end end
(node, ?yielded: Set[node]) { (node) → void } → void
(node) → Enumerator[node, void]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ancestor_graph.rb, line 78 def each_descendant(node, yielded: Set[], &block) if block each_child(node) do |child| unless yielded.member?(child) yielded << child yield child each_descendant(child, yielded: yielded, &block) end end else enum_for :each_descendant, node end end
(node) { (node) → void } → void
(node) → Enumerator[node, void]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ancestor_graph.rb, line 48 def each_parent(node, &block) if block parents[node]&.each(&block) else enum_for :each_parent, node end end
(parent: node, child: node) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/ancestor_graph.rb, line 43 def register(parent:, child:) (parents[child] ||= Set[]) << parent (children[parent] ||= Set[]) << child end