Class: Synthra::Parser::AST::FieldNode

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

Overview

Field definition node

Represents a single field within a schema. Contains the field name, type information, optional flag, conditions, and behaviors.

Examples:

DSL

email?: email if has_contact  @partial_data 20%

AST

FieldNode.new(
  name: "email",
  type_node: TypeNode.new(name: "email"),
  optional: true,
  condition: ConditionNode.new(field_name: "has_contact"),
  behaviors: [BehaviorNode.new(name: :partial_data, value: 20)]
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type_node:, optional: false, condition: nil, behaviors: [], line: nil) ⇒ FieldNode

Create a new FieldNode

Parameters:

  • name (String)

    field name (without ?)

  • type_node (TypeNode)

    type definition

  • optional (Boolean) (defaults to: false)

    is field optional?

  • condition (ConditionNode, nil) (defaults to: nil)

    inclusion condition

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

    field behaviors

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

    source line number



240
241
242
243
244
245
246
247
# File 'lib/synthra/parser/ast.rb', line 240

def initialize(name:, type_node:, optional: false, condition: nil, behaviors: [], line: nil)
  super(line: line)
  @name = name
  @type_node = type_node
  @optional = optional
  @condition = condition
  @behaviors = behaviors
end

Instance Attribute Details

#behaviorsObject (readonly)

Returns the value of attribute behaviors.



228
229
230
# File 'lib/synthra/parser/ast.rb', line 228

def behaviors
  @behaviors
end

#conditionObject (readonly)

Returns the value of attribute condition.



222
223
224
# File 'lib/synthra/parser/ast.rb', line 222

def condition
  @condition
end

#nameObject (readonly)

Returns the value of attribute name.



204
205
206
# File 'lib/synthra/parser/ast.rb', line 204

def name
  @name
end

#optionalObject (readonly)

Returns the value of attribute optional.



216
217
218
# File 'lib/synthra/parser/ast.rb', line 216

def optional
  @optional
end

#type_nodeObject (readonly)

Returns the value of attribute type_node.



210
211
212
# File 'lib/synthra/parser/ast.rb', line 210

def type_node
  @type_node
end

Instance Method Details

#optional?Boolean

Check if this field is optional

Returns:

  • (Boolean)

    true if the field is optional



254
255
256
# File 'lib/synthra/parser/ast.rb', line 254

def optional?
  @optional
end

#to_fieldSynthra::Field

Convert this AST node to a Field object

Creates a Synthra::Field instance from this node.

Examples:

field_node.to_field
# => #<Field email?: email>

Returns:



269
270
271
272
273
274
275
276
277
278
# File 'lib/synthra/parser/ast.rb', line 269

def to_field
  Field.new(
    name: optional? ? "#{name}?" : name,
    type_name: type_node.name,
    type_args: type_node.arguments,
    nullable: type_node.nullable?,
    condition: condition&.field_name,
    behaviors: behaviors.map(&:to_field_behavior)
  )
end