Class: RailsOpenapiGen::AstNodes::BaseNode

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-openapi-gen/ast_nodes/base_node.rb

Overview

Base class for all AST nodes in the Jbuilder parser Provides common interface and basic functionality for tree structure

Direct Known Subclasses

ArrayNode, ObjectNode, PartialNode, PropertyNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, metadata: {}) ⇒ BaseNode

Returns a new instance of BaseNode.



11
12
13
14
15
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 11

def initialize(parent: nil, metadata: {})
  @parent = parent
  @children = []
   = 
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 9

def children
  @children
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 9

def 
  
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 8

def parent
  @parent
end

Instance Method Details

#accept(visitor) ⇒ Object

Accept a visitor for visitor pattern implementation

Parameters:

  • visitor (Object)

    Visitor object

Returns:

  • (Object)

    Result from visitor



77
78
79
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 77

def accept(visitor)
  visitor.visit(self)
end

#add_child(child) ⇒ BaseNode

Add a child node to this node

Parameters:

  • child (BaseNode)

    Child node to add

Returns:



20
21
22
23
24
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 20

def add_child(child)
  child.parent = self if child.respond_to?(:parent=)
  @children << child
  child
end

#debug_lineString

Print just this node without children

Returns:

  • (String)

    Single line representation



98
99
100
101
102
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 98

def debug_line
  node_name = self.class.name.split('::').last
  summary = summary_attributes
  "#{node_name}#{summary.empty? ? '' : " (#{summary})"}"
end

#descendantsArray<BaseNode>

Get all descendants (children and their children recursively)

Returns:

  • (Array<BaseNode>)

    All descendant nodes



35
36
37
38
39
40
41
42
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 35

def descendants
  result = []
  @children.each do |child|
    result << child
    result.concat(child.descendants) if child.respond_to?(:descendants)
  end
  result
end

#leaf?Boolean

Check if this node is a leaf (has no children)

Returns:

  • (Boolean)

    True if this node has no children



46
47
48
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 46

def leaf?
  @children.empty?
end

#pretty_print(indent = 0) ⇒ void

This method returns an undefined value.

Pretty print the AST tree structure for debugging

Parameters:

  • indent (Integer) (defaults to: 0)

    Current indentation level



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 84

def pretty_print(indent = 0)
  pad = '  ' * indent
  node_name = self.class.name.split('::').last
  summary = summary_attributes

  puts "#{pad}#{tree_symbol(indent)} #{node_name}#{summary.empty? ? '' : " (#{summary})"}"

  @children.each_with_index do |child, _index|
    child.pretty_print(indent + 1) if child.respond_to?(:pretty_print)
  end
end

#remove_child(child) ⇒ BaseNode?

Remove a child node from this node

Parameters:

  • child (BaseNode)

    Child node to remove

Returns:

  • (BaseNode, nil)

    The removed child node or nil



29
30
31
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 29

def remove_child(child)
  @children.delete(child)
end

#rootBaseNode

Get the root node of the tree

Returns:



58
59
60
61
62
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 58

def root
  return self if root?

  @parent.root
end

#root?Boolean

Check if this node is the root (has no parent)

Returns:

  • (Boolean)

    True if this node has no parent



52
53
54
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 52

def root?
  @parent.nil?
end

#summary_attributesString

Generate summary attributes for debugging display

Returns:

  • (String)

    Summary of key attributes



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 106

def summary_attributes
  attrs = []

  # Common attributes that most nodes might have
  attrs << "name=#{property_name}" if respond_to?(:property_name) && property_name

  if respond_to?(:comment_data) && comment_data
    attrs << "type=#{comment_data.type}" if comment_data.type
    attrs << "required=#{comment_data.required?}" if comment_data.respond_to?(:required?)
    attrs << "desc=#{comment_data.description[0..30]}..." if comment_data.description && comment_data.description.length > 30
    attrs << "desc=#{comment_data.description}" if comment_data.description && comment_data.description.length <= 30
  end

  attrs << "children=#{@children.size}" if @children.size > 0
  attrs << "conditional" if respond_to?(:is_conditional) && is_conditional

  attrs.join(', ')
end

#to_hHash

Convert node to hash representation

Returns:

  • (Hash)

    Hash representation of the node



66
67
68
69
70
71
72
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 66

def to_h
  {
    node_type: self.class.name.split('::').last.downcase.gsub('node', ''),
    metadata: ,
    children: @children.map { |child| child.respond_to?(:to_h) ? child.to_h : child }
  }
end