Class: TL1::AST::CommaSeparatedKeywordVariables
- Defined in:
- lib/tl1/ast.rb
Overview
A group of fields separated by commas and identified by keywords. Fields may appear in any order.
Instance Attribute Summary
Attributes inherited from Node
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #format(**kwargs) ⇒ Object
-
#initialize(fields) ⇒ CommaSeparatedKeywordVariables
constructor
A new instance of CommaSeparatedKeywordVariables.
- #parse(fragment, record:) ⇒ Object
Methods inherited from Node
Constructor Details
#initialize(fields) ⇒ CommaSeparatedKeywordVariables
Returns a new instance of CommaSeparatedKeywordVariables.
189 190 191 |
# File 'lib/tl1/ast.rb', line 189 def initialize(fields) @fields = fields end |
Class Method Details
.parse(source) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# 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 |
Instance Method Details
#as_json ⇒ Object
185 186 187 |
# File 'lib/tl1/ast.rb', line 185 def as_json super(fields.keys.zip(fields.values.map(&:as_json)).to_h) end |
#format(**kwargs) ⇒ Object
193 194 195 196 197 198 199 200 201 |
# 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:) ⇒ Object
203 204 205 206 207 208 209 210 211 212 |
# 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 |