Class: Synthra::Parser::AST::SchemaDefinitionNode

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

Overview

Schema definition node (new syntax)

Represents a schema definition using the new Simulyra syntax: schema Name:

This differs from the legacy SchemaNode which just uses Name:

Examples:

DSL

schema User:
  id: uuid
  email: email

AST

SchemaDefinitionNode.new(
  name: "User",
  fields: [FieldNode.new(...), FieldNode.new(...)]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new SchemaDefinitionNode

Parameters:

  • name (String)

    the schema name

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

    field definitions

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

    schema-level behaviors

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

    source line number



812
813
814
815
816
817
# File 'lib/synthra/parser/ast.rb', line 812

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

Instance Attribute Details

#behaviorsObject (readonly)

Returns the value of attribute behaviors.



802
803
804
# File 'lib/synthra/parser/ast.rb', line 802

def behaviors
  @behaviors
end

#fieldsObject (readonly)

Returns the value of attribute fields.



796
797
798
# File 'lib/synthra/parser/ast.rb', line 796

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



790
791
792
# File 'lib/synthra/parser/ast.rb', line 790

def name
  @name
end

Instance Method Details

#to_schemaSynthra::Schema

Convert to Schema object

Returns:



824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/synthra/parser/ast.rb', line 824

def to_schema
  all_behaviors = behaviors.map(&:to_behavior)
  
  version_behavior = all_behaviors.find { |b| b[:name] == :version }
  version = version_behavior&.dig(:value)
  
  deprecated_behavior = all_behaviors.find { |b| b[:name] == :deprecated }
  deprecated = !deprecated_behavior.nil?
  deprecation_message = deprecated_behavior&.dig(:value)
  
  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