Class: RailsOpenapiGen::AstNodes::ObjectNode
- Defined in:
- lib/rails-openapi-gen/ast_nodes/object_node.rb
Overview
Represents an object node in Jbuilder template (json.object do...end)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#comment_data ⇒ Object
readonly
json.xxx # @openapi comments # if/else.
-
#is_conditional ⇒ Object
readonly
json.xxx # @openapi comments # if/else.
-
#property_name ⇒ Object
readonly
json.xxx # @openapi comments # if/else.
Attributes inherited from BaseNode
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
Accept visitor for visitor pattern.
-
#add_property(property_node) ⇒ BaseNode
Add a property to this object.
-
#description ⇒ String?
Get the description for this object.
-
#find_property(name) ⇒ BaseNode?
Find a property by name.
-
#has_properties? ⇒ Boolean
Check if object has any properties.
-
#initialize(property_name:, comment_data: nil, is_conditional: false, parent: nil, metadata: {}) ⇒ ObjectNode
constructor
A new instance of ObjectNode.
-
#optional? ⇒ Boolean
Check if this object is optional in OpenAPI schema.
-
#optional_properties ⇒ Array<String>
Get optional properties for OpenAPI schema.
-
#properties ⇒ Array<BaseNode>
Get all properties in this object.
-
#required? ⇒ Boolean
Check if this object is required in OpenAPI schema.
-
#required_properties ⇒ Array<String>
Get required properties for OpenAPI schema.
-
#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:, comment_data: nil, is_conditional: false, parent: nil, metadata: {}) ⇒ ObjectNode
Returns a new instance of ObjectNode.
8 9 10 11 12 13 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 8 def initialize(property_name:, comment_data: nil, is_conditional: false, parent: nil, metadata: {}) super(parent: parent, metadata: ) @property_name = property_name @comment_data = comment_data || CommentData.new(type: 'object') @is_conditional = is_conditional end |
Instance Attribute Details
#comment_data ⇒ Object (readonly)
json.xxx # @openapi comments # if/else
6 7 8 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 6 def comment_data @comment_data end |
#is_conditional ⇒ Object (readonly)
json.xxx # @openapi comments # if/else
6 7 8 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 6 def is_conditional @is_conditional end |
#property_name ⇒ Object (readonly)
json.xxx # @openapi comments # if/else
6 7 8 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 6 def property_name @property_name end |
Instance Method Details
#accept(visitor) ⇒ Object
Accept visitor for visitor pattern
125 126 127 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 125 def accept(visitor) visitor.visit_object(self) end |
#add_property(property_node) ⇒ BaseNode
Add a property to this object
18 19 20 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 18 def add_property(property_node) add_child(property_node) end |
#description ⇒ String?
Get the description for this object
82 83 84 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 82 def description @comment_data.description end |
#find_property(name) ⇒ BaseNode?
Find a property by name
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 89 def find_property(name) properties.find do |prop| if prop.respond_to?(:property_name) prop.property_name == name else # Handle legacy Hash objects (prop[:property_name] || prop[:property]) == name end end end |
#has_properties? ⇒ Boolean
Check if object has any properties
102 103 104 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 102 def has_properties? !properties.empty? end |
#optional? ⇒ Boolean
Check if this object is optional in OpenAPI schema
76 77 78 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 76 def optional? !required? end |
#optional_properties ⇒ Array<String>
Get optional properties for OpenAPI schema
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 50 def optional_properties properties.select do |property| if property.respond_to?(:optional?) property.optional? else # Handle legacy Hash objects property.is_a?(Hash) && property[:required] != true end end.map do |property| if property.respond_to?(:property_name) property.property_name else # Handle legacy Hash objects property[:property_name] || property[:property] end end.compact end |
#properties ⇒ Array<BaseNode>
Get all properties in this object
24 25 26 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 24 def properties @children end |
#required? ⇒ Boolean
Check if this object is required in OpenAPI schema
70 71 72 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 70 def required? @comment_data.required? && !@is_conditional end |
#required_properties ⇒ Array<String>
Get required properties for OpenAPI schema
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 30 def required_properties properties.select do |property| if property.respond_to?(:required?) property.required? else # Handle legacy Hash objects property.is_a?(Hash) && property[:required] == true end end.map do |property| if property.respond_to?(:property_name) property.property_name else # Handle legacy Hash objects property[:property_name] || property[:property] end end.compact end |
#to_h ⇒ Hash
Convert to hash representation
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rails-openapi-gen/ast_nodes/object_node.rb', line 108 def to_h super.merge( property_name: @property_name, comment_data: @comment_data&.to_h, is_conditional: @is_conditional, required: required?, openapi_type: 'object', description: description, properties: properties.map { |prop| prop.respond_to?(:to_h) ? prop.to_h : prop }, required_properties: required_properties, optional_properties: optional_properties ).compact end |