Class: RailsOpenapiGen::AstNodes::ArrayNode
- Defined in:
- lib/rails-openapi-gen/ast_nodes/array_node.rb
Overview
Represents an array node in Jbuilder template (json.array! or json.property @collection)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#comment_data ⇒ Object
readonly
Returns the value of attribute comment_data.
-
#is_conditional ⇒ Object
readonly
Returns the value of attribute is_conditional.
-
#is_root_array ⇒ Object
readonly
Returns the value of attribute is_root_array.
-
#property_name ⇒ Object
readonly
Returns the value of attribute property_name.
Attributes inherited from BaseNode
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
Accept visitor for visitor pattern.
-
#add_item(item_node) ⇒ BaseNode
Add an item property to this array.
-
#description ⇒ String?
Get the description for this array.
-
#initialize(property_name: nil, comment_data: nil, is_conditional: false, is_root_array: false, parent: nil, metadata: {}) ⇒ ArrayNode
constructor
A new instance of ArrayNode.
-
#item_type ⇒ String
Get the OpenAPI type for array items.
-
#items ⇒ Array<BaseNode>
Get all item properties in this array.
-
#optional? ⇒ Boolean
Check if this array is optional in OpenAPI schema.
-
#required? ⇒ Boolean
Check if this array is required in OpenAPI schema.
-
#root_array? ⇒ Boolean
Check if this is a root array (json.array! at template root).
-
#to_h ⇒ Hash
Convert to hash representation.
Methods inherited from BaseNode
#add_child, #debug_line, #descendants, #leaf?, #pretty_print, #remove_child, #root, #root?, #summary_attributes
Constructor Details
#initialize(property_name: nil, comment_data: nil, is_conditional: false, is_root_array: false, parent: nil, metadata: {}) ⇒ ArrayNode
Returns a new instance of ArrayNode.
8 9 10 11 12 13 14 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 8 def initialize(property_name: nil, comment_data: nil, is_conditional: false, is_root_array: false, parent: nil, metadata: {}) super(parent: parent, metadata: ) @property_name = property_name || (is_root_array ? 'items' : nil) @comment_data = comment_data || CommentData.new(type: 'array') @is_conditional = is_conditional @is_root_array = is_root_array end |
Instance Attribute Details
#comment_data ⇒ Object (readonly)
Returns the value of attribute comment_data.
6 7 8 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 6 def comment_data @comment_data end |
#is_conditional ⇒ Object (readonly)
Returns the value of attribute is_conditional.
6 7 8 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 6 def is_conditional @is_conditional end |
#is_root_array ⇒ Object (readonly)
Returns the value of attribute is_root_array.
6 7 8 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 6 def is_root_array @is_root_array end |
#property_name ⇒ Object (readonly)
Returns the value of attribute property_name.
6 7 8 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 6 def property_name @property_name end |
Instance Method Details
#accept(visitor) ⇒ Object
Accept visitor for visitor pattern
97 98 99 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 97 def accept(visitor) visitor.visit_array(self) end |
#add_item(item_node) ⇒ BaseNode
Add an item property to this array
19 20 21 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 19 def add_item(item_node) add_child(item_node) end |
#description ⇒ String?
Get the description for this array
63 64 65 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 63 def description @comment_data.description end |
#item_type ⇒ String
Get the OpenAPI type for array items
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 43 def item_type return 'object' if items.any? # Check comment data for item type specification if @comment_data.items case @comment_data.items when Hash @comment_data.items[:type] || 'object' when String @comment_data.items else 'object' end else 'object' end end |
#items ⇒ Array<BaseNode>
Get all item properties in this array
25 26 27 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 25 def items @children end |
#optional? ⇒ Boolean
Check if this array is optional in OpenAPI schema
37 38 39 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 37 def optional? !required? end |
#required? ⇒ Boolean
Check if this array is required in OpenAPI schema
31 32 33 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 31 def required? @comment_data.required? && !@is_conditional end |
#root_array? ⇒ Boolean
Check if this is a root array (json.array! at template root)
69 70 71 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 69 def root_array? @is_root_array end |
#to_h ⇒ Hash
Convert to hash representation
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 75 def to_h items_hash = items.map { |item| item.respond_to?(:to_h) ? item.to_h : item } super.merge( property_name: @property_name, comment_data: @comment_data&.to_h, is_conditional: @is_conditional, is_root_array: @is_root_array, required: required?, openapi_type: 'array', item_type: item_type, description: description, items: items_hash, # Backward compatibility - also provide array_item_properties for Generator array_item_properties: items_hash, # Backward compatibility - also provide is_array_root for Generator is_array_root: @is_root_array ).compact end |