Class: RailsOpenapiGen::AstNodes::CommentData

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#conditionalObject (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

#descriptionObject (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

#enumObject (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

#exampleObject (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_nameObject (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

#formatObject (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

#itemsObject (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

#requiredObject (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

#typeObject (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_itemsHash?

Get items specification for arrays

Returns:

  • (Hash, nil)

    Items specification



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

Returns:

  • (Boolean)

    True if 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_valueString?

Get format for the property, including auto-detected formats

Returns:

  • (String, nil)

    Format specification



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

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if property has 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

Returns:

  • (Boolean)

    True if property has format specification



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

Parameters:

Returns:



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_typeString

Get OpenAPI type, defaulting to string if not specified

Returns:

  • (String)

    OpenAPI type



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

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if 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_hHash

Convert to hash representation for internal use

Returns:

  • (Hash)

    Hash representation



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_schemaHash

Convert to hash representation suitable for OpenAPI schema

Returns:

  • (Hash)

    Hash representation



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

Parameters:

  • attributes (Hash)

    Attributes to update

Returns:

  • (CommentData)

    New comment data 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