Class: Synthra::Parser::AST::TypeNode

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

Overview

Type reference node

Represents a type declaration with optional arguments. This is the base class for type nodes; ArrayTypeNode and CustomNode extend it for special cases.

Examples:

DSL

age: number(18..80)
status: enum(active, inactive:30%)

AST

TypeNode.new(
  name: "number",
  arguments: { range: 18..80 }
)

Direct Known Subclasses

ArrayTypeNode, CustomNode, SchemaReferenceNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, arguments: {}, nullable: false, line: nil) ⇒ TypeNode

Create a new TypeNode

Parameters:

  • name (String)

    type name (may have ? suffix)

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

    type arguments

  • nullable (Boolean) (defaults to: false)

    is type nullable?

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

    source line number



326
327
328
329
330
331
# File 'lib/synthra/parser/ast.rb', line 326

def initialize(name:, arguments: {}, nullable: false, line: nil)
  super(line: line)
  @name = name.to_s.delete_suffix("?")
  @nullable = nullable || name.to_s.end_with?("?")
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



310
311
312
# File 'lib/synthra/parser/ast.rb', line 310

def arguments
  @arguments
end

#nameObject (readonly)

Returns the value of attribute name.



304
305
306
# File 'lib/synthra/parser/ast.rb', line 304

def name
  @name
end

#nullableObject (readonly)

Returns the value of attribute nullable.



316
317
318
# File 'lib/synthra/parser/ast.rb', line 316

def nullable
  @nullable
end

Instance Method Details

#nullable?Boolean

Check if this type can be null

Returns:

  • (Boolean)

    true if nullable



338
339
340
# File 'lib/synthra/parser/ast.rb', line 338

def nullable?
  @nullable
end