Class: Janeway::Interpreters::ChildSegmentInterpreter

Inherits:
Base
  • Object
show all
Defined in:
lib/janeway/interpreters/child_segment_interpreter.rb

Overview

For each node in the input nodelist, the resulting nodelist of a child segment is the concatenation of the nodelists from each of its selectors in the order that the selectors appear in the list. Note: Any node matched by more than one selector is kept as many times in the nodelist.

A child segment can only contain selectors according to the RFC’s ABNF grammar, so it cannot contain another child segment, or a new expression starting with the root identifier.

Direct Known Subclasses

ChildSegmentDeleteIf, ChildSegmentDeleter

Constant Summary

Constants inherited from Base

Base::NOTHING

Instance Attribute Summary

Attributes inherited from Base

#next, #node

Instance Method Summary collapse

Methods inherited from Base

#selector, #to_s, #type

Constructor Details

#initialize(child_segment) ⇒ ChildSegmentInterpreter

Returns a new instance of ChildSegmentInterpreter.

Parameters:



16
17
18
19
20
21
22
# File 'lib/janeway/interpreters/child_segment_interpreter.rb', line 16

def initialize(child_segment)
  super
  @selectors =
    child_segment.map do |expr|
      TreeConstructor.ast_node_to_interpreter(expr)
    end
end

Instance Method Details

#as_jsonHash

Return hash representation of this interpreter

Returns:

  • (Hash)


60
61
62
63
64
65
66
# File 'lib/janeway/interpreters/child_segment_interpreter.rb', line 60

def as_json
  {
    type: type,
    selectors: @selectors.map(&:as_json),
    next: @next&.as_json,
  }
end

#interpret(input, parent, root, path) ⇒ Array

Interpret a set of 2 or more selectors, seperated by the union operator. All selectors are sent identical input, the results are combined.

Parameters:

  • input (Array, Hash)

    the results of processing so far

  • parent (Array, Hash)

    parent of the input object

  • root (Array, Hash)

    the entire input

  • path (Array<String>)

    elements of normalized path to the current input

Returns:

  • (Array)


48
49
50
51
52
53
54
55
56
# File 'lib/janeway/interpreters/child_segment_interpreter.rb', line 48

def interpret(input, parent, root, path)
  # Apply each expression to the input, collect results
  results = []
  @selectors.each do |selector|
    results.concat selector.interpret(input, parent, root, path)
  end

  results
end

#push(node) ⇒ Object

Append an interpreter onto each of the selector branches

Parameters:



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/janeway/interpreters/child_segment_interpreter.rb', line 26

def push(node)
  return unless node

  node.next = nil
  @selectors.each do |selector|
    last_node = find_last(selector)
    if last_node.is_a?(Interpreters::ChildSegmentInterpreter)
      last_node.push(node.dup)
    else
      last_node.next = node.dup
    end
  end
end