Class: Janeway::Interpreters::ChildSegmentInterpreter
- 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
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#as_json ⇒ Hash
Return hash representation of this interpreter.
-
#initialize(child_segment) ⇒ ChildSegmentInterpreter
constructor
A new instance of ChildSegmentInterpreter.
-
#interpret(input, parent, root, path) ⇒ Array
Interpret a set of 2 or more selectors, seperated by the union operator.
-
#push(node) ⇒ Object
Append an interpreter onto each of the selector branches.
Methods inherited from Base
Constructor Details
#initialize(child_segment) ⇒ ChildSegmentInterpreter
Returns a new instance of ChildSegmentInterpreter.
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_json ⇒ Hash
Return hash representation of this interpreter
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.
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
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 |