Class: RailsOpenapiGen::AstNodes::PartialNode

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

Overview

Represents a partial node in Jbuilder template (json.partial!)

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(partial_path:, property_name: nil, comment_data: nil, is_conditional: false, local_variables: {}, parent: nil, metadata: {}) ⇒ PartialNode

Returns a new instance of PartialNode.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 8

def initialize(
  partial_path:,
  property_name: nil,
  comment_data: nil,
  is_conditional: false,
  local_variables: {},
  parent: nil,
  metadata: {}
)
  super(parent: parent, metadata: )
  @partial_path = partial_path
  @property_name = property_name
  @comment_data = comment_data || CommentData.new
  @is_conditional = is_conditional
  @local_variables = local_variables
end

Instance Attribute Details

#comment_dataObject (readonly)

Returns the value of attribute comment_data.



6
7
8
# File 'lib/rails-openapi-gen/ast_nodes/partial_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/partial_node.rb', line 6

def is_conditional
  @is_conditional
end

#local_variablesObject (readonly)

Returns the value of attribute local_variables.



6
7
8
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 6

def local_variables
  @local_variables
end

#partial_pathObject (readonly)

Returns the value of attribute partial_path.



6
7
8
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 6

def partial_path
  @partial_path
end

#property_nameObject (readonly)

Returns the value of attribute property_name.



6
7
8
# File 'lib/rails-openapi-gen/ast_nodes/partial_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



107
108
109
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 107

def accept(visitor)
  visitor.visit_partial(self)
end

#add_parsed_properties(properties) ⇒ Array<BaseNode>

Add parsed properties from the partial

Parameters:

  • properties (Array<BaseNode>)

    Properties from parsed partial

Returns:

  • (Array<BaseNode>)

    Added properties



78
79
80
81
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 78

def add_parsed_properties(properties)
  properties.each { |prop| add_child(prop) }
  properties
end

#descriptionString?

Get the description for this partial

Returns:

  • (String, nil)

    Partial description



52
53
54
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 52

def description
  @comment_data.description
end

#has_locals?Boolean

Check if partial has local variables

Returns:

  • (Boolean)

    True if partial has local variables



58
59
60
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 58

def has_locals?
  !@local_variables.empty?
end

#local_namesArray<String>

Get local variable names

Returns:

  • (Array<String>)

    Local variable names



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

def local_names
  @local_variables.keys
end

#local_value(name) ⇒ Object

Get value for a local variable

Parameters:

  • name (String)

    Local variable name

Returns:

  • (Object)

    Local variable value



71
72
73
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 71

def local_value(name)
  @local_variables[name]
end

#optional?Boolean

Check if this partial is optional in OpenAPI schema

Returns:

  • (Boolean)

    True if partial is optional



46
47
48
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 46

def optional?
  !required?
end

#propertiesArray<BaseNode>

Get all properties from the parsed partial

Returns:

  • (Array<BaseNode>)

    Properties from partial



85
86
87
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 85

def properties
  @children
end

#required?Boolean

Check if this partial is required in OpenAPI schema

Returns:

  • (Boolean)

    True if partial is required



40
41
42
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 40

def required?
  @comment_data.required? && !@is_conditional
end

#resolve_path(base_path = nil) ⇒ String

Resolve the absolute path of the partial

Parameters:

  • base_path (String) (defaults to: nil)

    Base path for resolution

Returns:

  • (String)

    Resolved partial path



28
29
30
31
32
33
34
35
36
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 28

def resolve_path(base_path = nil)
  return @partial_path if @partial_path.start_with?('/')

  if base_path
    File.join(File.dirname(base_path), "#{@partial_path}.json.jbuilder")
  else
    "#{@partial_path}.json.jbuilder"
  end
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)

    Hash representation



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rails-openapi-gen/ast_nodes/partial_node.rb', line 91

def to_h
  super.merge(
    partial_path: @partial_path,
    property_name: @property_name,
    comment_data: @comment_data&.to_h,
    is_conditional: @is_conditional,
    required: required?,
    description: description,
    local_variables: @local_variables,
    properties: properties.map { |prop| prop.respond_to?(:to_h) ? prop.to_h : prop }
  ).compact
end