Class: Yard2steep::AST::ClassNode

Inherits:
Object
  • Object
show all
Defined in:
lib/yard2steep/ast/class_node.rb

Overview

AST::ClassNode represents ‘Class` or `Module` AST.

Constant Summary collapse

KIND =
['class', 'module']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, c_name:, super_c:, parent:) ⇒ ClassNode

Returns a new instance of ClassNode.

Parameters:

  • kind (String)
  • c_name (String)
  • parent (AST::ClassNode, nil)
  • super_c (String, nil)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yard2steep/ast/class_node.rb', line 24

def initialize(kind:, c_name:, super_c:, parent:)
  Util.assert! { KIND.include?(kind) }
  Util.assert! { c_name.is_a?(String) }
  Util.assert! {
    parent.is_a?(AST::ClassNode) ||
    (parent == nil && c_name == 'main')
  }
  @kind       = kind
  @c_name     = c_name
  @super_c    = super_c
  @c_list     = []  # list of constants
  @m_list     = []  # list of methods
  @ivar_list  = []  # list of instance variables
  @ivarname_s = Set.new
  @children   = []  # list of child classes
  @parent     = parent
end

Instance Attribute Details

#c_listObject (readonly)

Returns the value of attribute c_list.



16
17
18
# File 'lib/yard2steep/ast/class_node.rb', line 16

def c_list
  @c_list
end

#c_nameObject (readonly)

Returns the value of attribute c_name.



16
17
18
# File 'lib/yard2steep/ast/class_node.rb', line 16

def c_name
  @c_name
end

#childrenObject (readonly)

Returns the value of attribute children.



16
17
18
# File 'lib/yard2steep/ast/class_node.rb', line 16

def children
  @children
end

#ivar_listObject (readonly)

Returns the value of attribute ivar_list.



16
17
18
# File 'lib/yard2steep/ast/class_node.rb', line 16

def ivar_list
  @ivar_list
end

#kindObject (readonly)

Returns the value of attribute kind.



16
17
18
# File 'lib/yard2steep/ast/class_node.rb', line 16

def kind
  @kind
end

#m_listObject (readonly)

Returns the value of attribute m_list.



16
17
18
# File 'lib/yard2steep/ast/class_node.rb', line 16

def m_list
  @m_list
end

#parentObject (readonly)

Returns the value of attribute parent.



16
17
18
# File 'lib/yard2steep/ast/class_node.rb', line 16

def parent
  @parent
end

#super_cObject (readonly)

Returns the value of attribute super_c.



16
17
18
# File 'lib/yard2steep/ast/class_node.rb', line 16

def super_c
  @super_c
end

Class Method Details

.create_mainAST::ClassNode

Returns:



6
7
8
9
10
11
12
13
# File 'lib/yard2steep/ast/class_node.rb', line 6

def self.create_main
  AST::ClassNode.new(
    kind:   'module',
    c_name: 'main',
    super_c: nil,
    parent:  nil,
  )
end

Instance Method Details

#append_child(child) ⇒ void

This method returns an undefined value.

Parameters:



65
66
67
# File 'lib/yard2steep/ast/class_node.rb', line 65

def append_child(child)
  @children.push(child)
end

#append_constant(c) ⇒ void

This method returns an undefined value.

Parameters:



44
45
46
# File 'lib/yard2steep/ast/class_node.rb', line 44

def append_constant(c)
  @c_list.push(c)
end

#append_ivar(ivar) ⇒ void

This method returns an undefined value.

Parameters:



56
57
58
59
60
61
# File 'lib/yard2steep/ast/class_node.rb', line 56

def append_ivar(ivar)
  if !@ivarname_s.include?(ivar.name)
    @ivar_list.push(ivar)
    @ivarname_s << ivar.name
  end
end

#append_m(m) ⇒ void

This method returns an undefined value.

Parameters:



50
51
52
# File 'lib/yard2steep/ast/class_node.rb', line 50

def append_m(m)
  @m_list.push(m)
end

#inspectString

Returns:

  • (String)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/yard2steep/ast/class_node.rb', line 99

def inspect
  <<-EOF
{
  #{@kind}: #{c_name},
  c_list: [#{@c_list.map(&:to_s).map { |s| "#{s}\n" }.join}],
  m_list: [#{@m_list.map(&:to_s).map { |s| "#{s}\n" }.join}],
  ivar_list: [#{@ivar_list.map(&:to_s).map { |s| "#{s}\n" }.join}],
  children: [#{@children.map(&:to_s).map { |s| "#{s}\n" }.join}],
}
  EOF
end

#long_nameString

Returns:

  • (String)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/yard2steep/ast/class_node.rb', line 70

def long_name
  @long_name ||= begin
     # NOTE: main has no long_name
     if @c_name == 'main'
       ''
     elsif @parent.c_name == 'main'
       @c_name
     else
       "#{@parent.long_name}::#{@c_name}"
     end
  end
end

#long_superObject



83
84
85
86
87
88
89
90
91
# File 'lib/yard2steep/ast/class_node.rb', line 83

def long_super
  @long_super ||= begin
     if @parent.c_name == 'main'
       @super_c
     else
       "#{@parent.long_name}::#{@super_c}"
     end
  end
end

#to_sString

Returns:

  • (String)


94
95
96
# File 'lib/yard2steep/ast/class_node.rb', line 94

def to_s
  inspect
end