Class: TL1::AST::ColonSeparatedVariables
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
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #format(**kwargs) ⇒ Object
-
#initialize(*fields) ⇒ ColonSeparatedVariables
constructor
A new instance of ColonSeparatedVariables.
- #parse(record_source, record: {}) ⇒ Object
Methods inherited from Node
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_json ⇒ Object
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 |