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. www.rfc-editor.org/rfc/rfc9535.html#child-segment

Examples:

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

Instance Attribute Summary collapse

Attributes inherited from Expression

#value

Instance Method Summary collapse

Methods inherited from Expression

#indented, #literal?, #singular_query?, #type

Constructor Details

#initializeChildSegment

Returns a new instance of ChildSegment.



23
24
25
# File 'lib/janeway/ast/child_segment.rb', line 23

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

Instance Attribute Details

#nextObject

Subsequent expression that modifies the result of this selector list.



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

def next
  @next
end

Instance Method Details

#<<(selector) ⇒ Object

Add a selector to the list

Raises:

  • (ArgumentError)


28
29
30
31
32
# File 'lib/janeway/ast/child_segment.rb', line 28

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

  @value << selector
end

#to_s(with_child: true) ⇒ Object



34
35
36
37
# File 'lib/janeway/ast/child_segment.rb', line 34

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

#tree(level) ⇒ Array

Parameters:

  • level (Integer)

Returns:

  • (Array)


41
42
43
44
# File 'lib/janeway/ast/child_segment.rb', line 41

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