Class: Synthra::Parser::AST::SchemaNode

Inherits:
Node
  • Object
show all
Defined in:
lib/synthra/parser/ast.rb

Overview

Schema definition node

Represents a complete schema definition in the DSL. A SchemaNode contains the schema name, list of fields, and any schema-level behaviors.

Examples:

DSL

User:
  @seed 12345
  id: uuid
  name: name

AST

SchemaNode.new(
  name: "User",
  fields: [FieldNode.new(...), FieldNode.new(...)],
  behaviors: [BehaviorNode.new(name: :seed, value: 12345)]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, fields: [], behaviors: [], parent_name: nil, line: nil) ⇒ SchemaNode

Create a new SchemaNode

Parameters:

  • name (String)

    the schema name

  • fields (Array<FieldNode>) (defaults to: [])

    field definitions

  • behaviors (Array<BehaviorNode>) (defaults to: [])

    schema-level behaviors

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

    name of an inherited parent schema

  • line (Integer, nil) (defaults to: nil)

    source line number



133
134
135
136
137
138
139
# File 'lib/synthra/parser/ast.rb', line 133

def initialize(name:, fields: [], behaviors: [], parent_name: nil, line: nil)
  super(line: line)
  @name = name
  @fields = fields
  @behaviors = behaviors
  @parent_name = parent_name
end

Instance Attribute Details

#behaviorsObject (readonly)

Returns the value of attribute behaviors.



116
117
118
# File 'lib/synthra/parser/ast.rb', line 116

def behaviors
  @behaviors
end

#fieldsObject (readonly)

Returns the value of attribute fields.



110
111
112
# File 'lib/synthra/parser/ast.rb', line 110

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



104
105
106
# File 'lib/synthra/parser/ast.rb', line 104

def name
  @name
end

#parent_nameObject (readonly)

Returns the value of attribute parent_name.



122
123
124
# File 'lib/synthra/parser/ast.rb', line 122

def parent_name
  @parent_name
end

Instance Method Details

#to_schemaSynthra::Schema

Convert this AST node to a Schema object

Creates a Synthra::Schema instance from this node, converting all child nodes (fields, behaviors) as well.

Examples:

schema_node.to_schema
# => #<Schema User fields=2 behaviors=1>

Returns:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/synthra/parser/ast.rb', line 153

def to_schema
  all_behaviors = behaviors.map(&:to_behavior)
  
  # Extract version from behaviors
  version_behavior = all_behaviors.find { |b| b[:name] == :version }
  version = version_behavior&.dig(:value)
  
  # Extract deprecated from behaviors
  deprecated_behavior = all_behaviors.find { |b| b[:name] == :deprecated }
  deprecated = !deprecated_behavior.nil?
  deprecation_message = deprecated_behavior&.dig(:value)
  
  # Filter out version/deprecated from runtime behaviors
  runtime_behaviors = all_behaviors.reject { |b| [:version, :deprecated].include?(b[:name]) }
  
  Schema.new(
    name: name,
    fields: fields.map(&:to_field),
    behaviors: runtime_behaviors,
    metadata: {},
    version: version,
    deprecated: deprecated,
    deprecation_message: deprecation_message
  )
end