Class: RBS::AncestorGraph

Inherits:
Object
  • Object
show all
Defined in:
sig/ancestor_graph.rbs,
lib/rbs/ancestor_graph.rb

Overview

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

Defined Under Namespace

Classes: InstanceNode, SingletonNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, ancestor_builder: DefinitionBuilder::AncestorBuilder.new(env: env)) ⇒ AncestorGraph

Returns a new instance of AncestorGraph.

Parameters:



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

Instance Attribute Details

#ancestor_builderDefinitionBuilder::AncestorBuilder (readonly)

Returns the value of attribute ancestor_builder.



9
10
11
# File 'lib/rbs/ancestor_graph.rb', line 9

def ancestor_builder
  @ancestor_builder
end

#childrenHash[node, Set[node]] (readonly)

Returns the value of attribute children.

Returns:

  • (Hash[node, Set[node]])


11
12
13
# File 'lib/rbs/ancestor_graph.rb', line 11

def children
  @children
end

#envEnvironment (readonly)

Returns the value of attribute env.

Returns:



8
9
10
# File 'lib/rbs/ancestor_graph.rb', line 8

def env
  @env
end

#parentsHash[node, Set[node]] (readonly)

Returns the value of attribute parents.

Returns:

  • (Hash[node, Set[node]])


10
11
12
# File 'lib/rbs/ancestor_graph.rb', line 10

def parents
  @parents
end

Instance Method Details

#buildvoid

This method returns an undefined value.



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

#build_ancestors(node, ancestors) ⇒ void

This method returns an undefined value.



32
33
34
35
36
37
38
39
40
41
# File '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

#each_ancestor(arg0, yielded:) ⇒ void #each_ancestor(arg0) ⇒ Enumerator[node, void]

Overloads:

  • #each_ancestor(arg0, yielded:) ⇒ void

    This method returns an undefined value.

    Parameters:

    • arg0 (node)
    • yielded: (Set[node])
  • #each_ancestor(arg0) ⇒ Enumerator[node, void]

    Parameters:

    • arg0 (node)

    Returns:

    • (Enumerator[node, void])

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


64
65
66
67
68
69
70
71
72
73
74
75
76
# File '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

#each_child(arg0) ⇒ void #each_child(arg0) ⇒ Enumerator[node, void]

Overloads:

  • #each_child(arg0) ⇒ void

    This method returns an undefined value.

    Parameters:

    • arg0 (node)
  • #each_child(arg0) ⇒ Enumerator[node, void]

    Parameters:

    • arg0 (node)

    Returns:

    • (Enumerator[node, void])

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


56
57
58
59
60
61
62
# File '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

#each_descendant(arg0, yielded:) ⇒ void #each_descendant(arg0) ⇒ Enumerator[node, void]

Overloads:

  • #each_descendant(arg0, yielded:) ⇒ void

    This method returns an undefined value.

    Parameters:

    • arg0 (node)
    • yielded: (Set[node])
  • #each_descendant(arg0) ⇒ Enumerator[node, void]

    Parameters:

    • arg0 (node)

    Returns:

    • (Enumerator[node, void])

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


78
79
80
81
82
83
84
85
86
87
88
89
90
# File '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

#each_parent(arg0) ⇒ void #each_parent(arg0) ⇒ Enumerator[node, void]

Overloads:

  • #each_parent(arg0) ⇒ void

    This method returns an undefined value.

    Parameters:

    • arg0 (node)
  • #each_parent(arg0) ⇒ Enumerator[node, void]

    Parameters:

    • arg0 (node)

    Returns:

    • (Enumerator[node, void])

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


48
49
50
51
52
53
54
# File '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

#register(parent:, child:) ⇒ void

This method returns an undefined value.

Parameters:

  • parent: (node)
  • child: (node)


43
44
45
46
# File 'lib/rbs/ancestor_graph.rb', line 43

def register(parent:, child:)
  (parents[child] ||= Set[]) << parent
  (children[parent] ||= Set[]) << child
end