Class: TL1::AST::CommaSeparatedVariables

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

Overview

A group of fields separated by commas with a fixed order.

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) ⇒ CommaSeparatedVariables

Returns a new instance of CommaSeparatedVariables.



147
148
149
# File 'lib/tl1/ast.rb', line 147

def initialize(*fields)
  @fields = fields
end

Class Method Details

.parse(source) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/tl1/ast.rb', line 136

def self.parse(source)
  elements =
    if source.is_a?(String)
      AST.split(source, ',').map { |e| Variable.parse(e) }
    else
      source.map { |e| Variable.parse(e['fields']) }
    end

  new(*elements)
end

Instance Method Details

#as_jsonObject



151
152
153
# File 'lib/tl1/ast.rb', line 151

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

#format(**kwargs) ⇒ Object



155
156
157
# File 'lib/tl1/ast.rb', line 155

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

#parse(source, record:) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/tl1/ast.rb', line 159

def parse(source, record:)
  AST.split(source, ',').zip(fields).each do |value, field|
    field.parse(value, record: record)
  end

  record
end