Class: RailsOpenapiGen::AstNodes::CommentData
- Inherits:
-
Object
- Object
- RailsOpenapiGen::AstNodes::CommentData
- Defined in:
- lib/rails-openapi-gen/ast_nodes/comment_data.rb
Overview
Represents comment data parsed from @openapi annotations Encapsulates all OpenAPI-related metadata extracted from comments
Instance Attribute Summary collapse
-
#conditional ⇒ Object
readonly
Returns the value of attribute conditional.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#enum ⇒ Object
readonly
Returns the value of attribute enum.
-
#example ⇒ Object
readonly
Returns the value of attribute example.
-
#field_name ⇒ Object
readonly
Returns the value of attribute field_name.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#array_items ⇒ Hash?
Get items specification for arrays.
-
#conditional? ⇒ Boolean
Check if the property is conditional.
-
#format_value ⇒ String?
Get format for the property, including auto-detected formats.
-
#has_enum? ⇒ Boolean
Check if the property has enum values.
-
#has_example? ⇒ Boolean
Check if the property has an example.
-
#has_format? ⇒ Boolean
Check if the property has a specific format.
-
#initialize(type: nil, description: nil, required: true, enum: nil, field_name: nil, items: nil, conditional: false, format: nil, example: nil) ⇒ CommentData
constructor
A new instance of CommentData.
-
#merge(other) ⇒ CommentData
Merge with another CommentData, giving precedence to non-nil values.
-
#openapi_type ⇒ String
Get OpenAPI type, defaulting to string if not specified.
-
#optional? ⇒ Boolean
Check if the property is optional.
-
#required? ⇒ Boolean
Check if the property is required.
-
#to_h ⇒ Hash
Convert to hash representation for internal use.
-
#to_openapi_schema ⇒ Hash
Convert to hash representation suitable for OpenAPI schema.
-
#with(**attributes) ⇒ CommentData
Create a copy with updated attributes.
Constructor Details
#initialize(type: nil, description: nil, required: true, enum: nil, field_name: nil, items: nil, conditional: false, format: nil, example: nil) ⇒ CommentData
Returns a new instance of CommentData.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 9 def initialize( type: nil, description: nil, required: true, enum: nil, field_name: nil, items: nil, conditional: false, format: nil, example: nil ) @type = type @description = description @required = required @enum = enum @field_name = field_name @items = items @conditional = conditional @format = format @example = example end |
Instance Attribute Details
#conditional ⇒ Object (readonly)
Returns the value of attribute conditional.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def conditional @conditional end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def description @description end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def enum @enum end |
#example ⇒ Object (readonly)
Returns the value of attribute example.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def example @example end |
#field_name ⇒ Object (readonly)
Returns the value of attribute field_name.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def field_name @field_name end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def format @format end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def items @items end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 7 def type @type end |
Instance Method Details
#array_items ⇒ Hash?
Get items specification for arrays
95 96 97 98 99 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 95 def array_items return nil unless @type == 'array' @items || { 'type' => 'object' } end |
#conditional? ⇒ Boolean
Check if the property is conditional
45 46 47 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 45 def conditional? @conditional == true || @conditional == 'true' end |
#format_value ⇒ String?
Get format for the property, including auto-detected formats
85 86 87 88 89 90 91 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 85 def format_value # Auto-detect format from invalid types that were converted auto_format = auto_format_from_invalid_type return auto_format if auto_format @format end |
#has_enum? ⇒ Boolean
Check if the property has enum values
51 52 53 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 51 def has_enum? @enum && !@enum.empty? end |
#has_example? ⇒ Boolean
Check if the property has an example
66 67 68 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 66 def has_example? @example end |
#has_format? ⇒ Boolean
Check if the property has a specific format
57 58 59 60 61 62 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 57 def has_format? # Auto-detect format from invalid types that were converted return true if auto_format_from_invalid_type @format && !@format.empty? end |
#merge(other) ⇒ CommentData
Merge with another CommentData, giving precedence to non-nil values
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 132 def merge(other) return self unless other.is_a?(CommentData) CommentData.new( type: other.type || @type, description: other.description || @description, required: other.required.nil? ? @required : other.required, enum: other.enum || @enum, field_name: other.field_name || @field_name, items: other.items || @items, conditional: other.conditional.nil? ? @conditional : other.conditional, format: other.format || @format, example: other.example || @example ) end |
#openapi_type ⇒ String
Get OpenAPI type, defaulting to string if not specified
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 72 def openapi_type # Handle common invalid types and auto-correct them case @type when 'date-time', 'datetime', 'date', 'time' # These are not valid OpenAPI types, should be string with format 'string' else @type || 'string' end end |
#optional? ⇒ Boolean
Check if the property is optional
39 40 41 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 39 def optional? !required? end |
#required? ⇒ Boolean
Check if the property is required
33 34 35 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 33 def required? @required != false && @required != 'false' end |
#to_h ⇒ Hash
Convert to hash representation for internal use
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 115 def to_h { type: @type, description: @description, required: @required, enum: @enum, field_name: @field_name, items: @items, conditional: @conditional, format: @format, example: @example }.compact end |
#to_openapi_schema ⇒ Hash
Convert to hash representation suitable for OpenAPI schema
103 104 105 106 107 108 109 110 111 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 103 def to_openapi_schema schema = { 'type' => openapi_type } schema['description'] = @description if @description schema['enum'] = @enum if has_enum? schema['format'] = format_value if has_format? schema['example'] = @example if has_example? schema['items'] = array_items if @type == 'array' && array_items schema end |
#with(**attributes) ⇒ CommentData
Create a copy with updated attributes
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rails-openapi-gen/ast_nodes/comment_data.rb', line 151 def with(**attributes) CommentData.new( type: attributes.fetch(:type, @type), description: attributes.fetch(:description, @description), required: attributes.fetch(:required, @required), enum: attributes.fetch(:enum, @enum), field_name: attributes.fetch(:field_name, @field_name), items: attributes.fetch(:items, @items), conditional: attributes.fetch(:conditional, @conditional), format: attributes.fetch(:format, @format), example: attributes.fetch(:example, @example) ) end |