Class: TL1::AST::ColonSeparatedVariables

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

Overview

A sequence of fields or groups of fields separated by colons. This is the “root” AST node for every input message and every record in an output message.

Instance Attribute Summary

Attributes inherited from Node

#fields

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#<=>, #to_json

Constructor Details

#initialize(*fields) ⇒ ColonSeparatedVariables

Returns a new instance of ColonSeparatedVariables.



111
112
113
# File 'lib/tl1/ast.rb', line 111

def initialize(*fields)
  @fields = fields
end

Class Method Details

.parse(source) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/tl1/ast.rb', line 100

def self.parse(source)
  elements =
    if source.is_a?(String)
      AST.split(source, ':')
    else
      source.fetch('fields')
    end

  new(*elements.map { |e| AST.colon_separated_element(e) })
end

Instance Method Details

#as_jsonObject



115
116
117
# File 'lib/tl1/ast.rb', line 115

def as_json
  super(@fields.map(&:as_json))
end

#format(**kwargs) ⇒ Object



119
120
121
# File 'lib/tl1/ast.rb', line 119

def format(**kwargs)
  fields.map { |f| f.format(**kwargs) }.join(':')
end

#parse(record_source, record: {}) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/tl1/ast.rb', line 123

def parse(record_source, record: {})
  pairs = AST.split(record_source, ':').zip(@fields)

  pairs.each do |fragment, node|
    node.parse(fragment, record: record)
  end

  record
end