Class: RailsOpenapiGen::AstNodes::BaseNode
- Inherits:
-
Object
- Object
- RailsOpenapiGen::AstNodes::BaseNode
- 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
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
Accept a visitor for visitor pattern implementation.
-
#add_child(child) ⇒ BaseNode
Add a child node to this node.
-
#debug_line ⇒ String
Print just this node without children.
-
#descendants ⇒ Array<BaseNode>
Get all descendants (children and their children recursively).
-
#initialize(parent: nil, metadata: {}) ⇒ BaseNode
constructor
A new instance of BaseNode.
-
#leaf? ⇒ Boolean
Check if this node is a leaf (has no children).
-
#pretty_print(indent = 0) ⇒ void
Pretty print the AST tree structure for debugging.
-
#remove_child(child) ⇒ BaseNode?
Remove a child node from this node.
-
#root ⇒ BaseNode
Get the root node of the tree.
-
#root? ⇒ Boolean
Check if this node is the root (has no parent).
-
#summary_attributes ⇒ String
Generate summary attributes for debugging display.
-
#to_h ⇒ Hash
Convert node to hash representation.
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
#children ⇒ Object (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 |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
9 10 11 |
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 9 def end |
#parent ⇒ Object
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
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
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_line ⇒ String
Print just this node without children
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 |
#descendants ⇒ Array<BaseNode>
Get all descendants (children and their children recursively)
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)
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
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
29 30 31 |
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 29 def remove_child(child) @children.delete(child) end |
#root ⇒ BaseNode
Get the root node of the tree
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)
52 53 54 |
# File 'lib/rails-openapi-gen/ast_nodes/base_node.rb', line 52 def root? @parent.nil? end |
#summary_attributes ⇒ String
Generate summary attributes for debugging display
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_h ⇒ Hash
Convert node to hash representation
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 |