Class: Janeway::AST::ChildSegment

Inherits:
Expression show all
Extended by:
Forwardable
Defined in:
lib/janeway/ast/child_segment.rb

Overview

Represent a union of 2 or more selectors.

A set of selectors within brackets, as a comma-separated list. https://www.rfc-editor.org/rfc/rfc9535.html#child-segment

Examples:

$[*, *]
$[1, 2, 3]
$[name1, [1:10]]

Instance Attribute Summary

Attributes inherited from Expression

#next, #value

Instance Method Summary collapse

Methods inherited from Expression

#chain_of?, #indented, #literal?, #singular_query?, #type, type_name

Constructor Details

#initializeChildSegment

Returns a new instance of ChildSegment.



21
22
23
# File 'lib/janeway/ast/child_segment.rb', line 21

def initialize
  super([]) # @value holds the expressions in the selector
end

Instance Method Details

#<<(selector) ⇒ Object

Add a selector to the list

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/janeway/ast/child_segment.rb', line 26

def <<(selector)
  raise ArgumentError, "expect Selector, got #{selector.inspect}" unless selector.is_a?(Selector)

  @value << selector
end

#to_s(with_child: true) ⇒ Object



32
33
34
35
# File 'lib/janeway/ast/child_segment.rb', line 32

def to_s(with_child: true, **)
  str = @value.map { |selector| selector.to_s(brackets: false, dot_prefix: false, bracketed: true) }.join(', ')
  with_child ? "[#{str}]#{@next}" : "[#{str}]"
end

#tree(level) ⇒ Array

Parameters:

  • level (Integer)

Returns:

  • (Array)


39
40
41
42
# File 'lib/janeway/ast/child_segment.rb', line 39

def tree(level)
  msg = format('[%s]', @value.map(&:to_s).join(', '))
  [indented(level, msg), @next&.tree(level + 1)]
end