Class: RailsOpenapiGen::AstNodes::NodeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-openapi-gen/ast_nodes/node_factory.rb

Overview

Factory class for creating AST nodes Provides convenient methods for creating different types of nodes

Direct Known Subclasses

PropertyNodeFactory

Class Method Summary collapse

Class Method Details

.create_array(property_name: nil, comment_data: nil, is_conditional: false, is_root_array: false, parent: nil) ⇒ ArrayNode

Create an array node

Parameters:

  • property_name (String, nil) (defaults to: nil)

    Name of the array property

  • comment_data (CommentData, Hash, nil) (defaults to: nil)

    Comment data

  • is_conditional (Boolean) (defaults to: false)

    Whether array is conditional

  • is_root_array (Boolean) (defaults to: false)

    Whether this is a root array (json.array!)

  • parent (BaseNode, nil) (defaults to: nil)

    Parent node

Returns:



34
35
36
37
38
39
40
41
42
# File 'lib/rails-openapi-gen/ast_nodes/node_factory.rb', line 34

def create_array(property_name: nil, comment_data: nil, is_conditional: false, is_root_array: false, parent: nil)
  ArrayNode.new(
    property_name: property_name,
    comment_data: normalize_comment_data(comment_data, default_type: 'array'),
    is_conditional: is_conditional,
    is_root_array: is_root_array,
    parent: parent
  )
end

.create_object(property_name:, comment_data: nil, is_conditional: false, parent: nil) ⇒ ObjectNode

Create an object node

Parameters:

  • property_name (String)

    Name of the object property

  • comment_data (CommentData, Hash, nil) (defaults to: nil)

    Comment data

  • is_conditional (Boolean) (defaults to: false)

    Whether object is conditional

  • parent (BaseNode, nil) (defaults to: nil)

    Parent node

Returns:



50
51
52
53
54
55
56
57
# File 'lib/rails-openapi-gen/ast_nodes/node_factory.rb', line 50

def create_object(property_name:, comment_data: nil, is_conditional: false, parent: nil)
  ObjectNode.new(
    property_name: property_name,
    comment_data: normalize_comment_data(comment_data, default_type: 'object'),
    is_conditional: is_conditional,
    parent: parent
  )
end

.create_partial(partial_path:, property_name: nil, comment_data: nil, is_conditional: false, local_variables: {}, parent: nil) ⇒ PartialNode

Create a partial node

Parameters:

  • partial_path (String)

    Path to the partial

  • property_name (String, nil) (defaults to: nil)

    Name of the property (for nested partials)

  • comment_data (CommentData, Hash, nil) (defaults to: nil)

    Comment data

  • is_conditional (Boolean) (defaults to: false)

    Whether partial is conditional

  • local_variables (Hash) (defaults to: {})

    Local variables passed to partial

  • parent (BaseNode, nil) (defaults to: nil)

    Parent node

Returns:



67
68
69
70
71
72
73
74
75
76
# File 'lib/rails-openapi-gen/ast_nodes/node_factory.rb', line 67

def create_partial(partial_path:, property_name: nil, comment_data: nil, is_conditional: false, local_variables: {}, parent: nil)
  PartialNode.new(
    partial_path: partial_path,
    property_name: property_name,
    comment_data: normalize_comment_data(comment_data),
    is_conditional: is_conditional,
    local_variables: local_variables,
    parent: parent
  )
end

.create_property(property_name:, comment_data: nil, is_conditional: false, is_component_ref: false, component_name: nil, parent: nil) ⇒ PropertyNode

Create a property node

Parameters:

  • property_name (String)

    Name of the property

  • comment_data (CommentData, Hash, nil) (defaults to: nil)

    Comment data

  • is_conditional (Boolean) (defaults to: false)

    Whether property is conditional

  • is_component_ref (Boolean) (defaults to: false)

    Whether this is a component reference

  • component_name (String, nil) (defaults to: nil)

    Name of the referenced component

  • parent (BaseNode, nil) (defaults to: nil)

    Parent node

Returns:



16
17
18
19
20
21
22
23
24
25
# File 'lib/rails-openapi-gen/ast_nodes/node_factory.rb', line 16

def create_property(property_name:, comment_data: nil, is_conditional: false, is_component_ref: false, component_name: nil, parent: nil)
  PropertyNode.new(
    property_name: property_name,
    comment_data: normalize_comment_data(comment_data),
    is_conditional: is_conditional,
    is_component_ref: is_component_ref,
    component_name: component_name,
    parent: parent
  )
end

.create_tree(hash_data) ⇒ BaseNode

Create a tree structure from nested hash data

Parameters:

  • hash_data (Hash)

    Root hash data

Returns:

  • (BaseNode)

    Root node with children



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rails-openapi-gen/ast_nodes/node_factory.rb', line 133

def create_tree(hash_data)
  root_node = from_hash(hash_data)

  # Add children if present
  if hash_data[:children]
    hash_data[:children].each do |child_data|
      child_node = create_tree(child_data)
      root_node.add_child(child_node)
    end
  end

  # Add specific child types
  add_specific_children(root_node, hash_data)

  root_node
end

.from_hash(hash_data) ⇒ BaseNode

Create a node from hash data (backward compatibility)

Parameters:

  • hash_data (Hash)

    Hash containing node data

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rails-openapi-gen/ast_nodes/node_factory.rb', line 81

def from_hash(hash_data)
  return hash_data unless hash_data.is_a?(Hash)

  case hash_data[:node_type]&.to_sym
  when :array
    create_array(
      property_name: hash_data[:property_name] || hash_data[:property],
      comment_data: hash_data[:comment_data],
      is_conditional: hash_data[:is_conditional] || false,
      is_root_array: hash_data[:is_root_array] || hash_data[:is_array_root] || false
    )
  when :object
    create_object(
      property_name: hash_data[:property_name] || hash_data[:property],
      comment_data: hash_data[:comment_data],
      is_conditional: hash_data[:is_conditional] || false
    )
  when :partial
    create_partial(
      partial_path: hash_data[:partial_path],
      property_name: hash_data[:property_name] || hash_data[:property],
      comment_data: hash_data[:comment_data],
      is_conditional: hash_data[:is_conditional] || false,
      local_variables: hash_data[:local_variables] || {}
    )
  else
    # Check if this is an array property that should be preserved as hash
    if hash_data[:is_array] && hash_data[:array_item_properties]
      # Return hash data directly to preserve array information
      hash_data
    else
      create_property(
        property_name: hash_data[:property_name] || hash_data[:property],
        comment_data: hash_data[:comment_data],
        is_conditional: hash_data[:is_conditional] || false
      )
    end
  end
end

.from_hash_array(hash_array) ⇒ Array<BaseNode>

Create nodes from an array of hash data

Parameters:

  • hash_array (Array<Hash>)

    Array of hash data

Returns:

  • (Array<BaseNode>)

    Array of created nodes



124
125
126
127
128
# File 'lib/rails-openapi-gen/ast_nodes/node_factory.rb', line 124

def from_hash_array(hash_array)
  return [] unless hash_array.is_a?(Array)

  hash_array.map { |hash_data| from_hash(hash_data) }
end