class TL1::AST::CommaSeparatedKeywordVariables

A group of fields separated by commas and identified by keywords. Fields may appear in any order.

Public Class Methods

new(fields) click to toggle source
# File lib/tl1/ast.rb, line 189
def initialize(fields)
  @fields = fields
end
parse(source) click to toggle source
# File lib/tl1/ast.rb, line 171
def self.parse(source)
  elements =
    if source.is_a?(String)
      AST.split(source, ',').map { |pair|
        key, value = pair.split('=', 2)
        [key.to_s, Variable.parse(value)]
      }.to_h
    else
      source.map { |k, v| [k.to_s, Variable.parse(v['fields'])] }.to_h
    end

  new(elements)
end

Public Instance Methods

as_json() click to toggle source
Calls superclass method TL1::AST::Node#as_json
# File lib/tl1/ast.rb, line 185
def as_json
  super(fields.keys.zip(fields.values.map(&:as_json)).to_h)
end
format(**kwargs) click to toggle source
# File lib/tl1/ast.rb, line 193
def format(**kwargs)
  fields.each_pair.flat_map { |keyword, variable|
    if kwargs.key?(variable.fields)
      ["#{keyword}=#{kwargs[variable.fields]}"]
    else
      []
    end
  }.join(',')
end
parse(fragment, record:) click to toggle source
# File lib/tl1/ast.rb, line 203
def parse(fragment, record:)
  AST.split(fragment, ',').each do |pair|
    next if pair.empty?
    key, value = pair.split('=', 2)
    field_name = @fields.fetch(key)
    record[field_name.fields] = AST.remove_quotes(value)
  end

  record
end