Class: RailsOpenapiGen::AstNodes::ArrayNode

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

ArrayPropertyNode, ArrayRootNode

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#children, #metadata, #parent

Instance Method Summary collapse

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_dataObject (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_conditionalObject (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_arrayObject (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_nameObject (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

Parameters:

  • visitor (Object)

    Visitor object

Returns:

  • (Object)

    Result from visitor



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

Parameters:

  • item_node (BaseNode)

    Item node to add

Returns:



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

#descriptionString?

Get the description for this array

Returns:

  • (String, nil)

    Array description



63
64
65
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 63

def description
  @comment_data.description
end

#item_typeString

Get the OpenAPI type for array items

Returns:

  • (String)

    OpenAPI type for 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

#itemsArray<BaseNode>

Get all item properties in this array

Returns:

  • (Array<BaseNode>)

    Array item nodes



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

Returns:

  • (Boolean)

    True if array is optional



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

Returns:

  • (Boolean)

    True if array is required



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)

Returns:

  • (Boolean)

    True if this is a root array



69
70
71
# File 'lib/rails-openapi-gen/ast_nodes/array_node.rb', line 69

def root_array?
  @is_root_array
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)

    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