Class: Janeway::Interpreters::DescendantSegmentInterpreter

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

Overview

Find all descendants of the current input that match the selector in the DescendantSegment

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

#as_json, #initialize, #selector, #to_s, #type

Constructor Details

This class inherits a constructor from Janeway::Interpreters::Base

Instance Method Details

#interpret(input, parent, root, path) ⇒ Array<AST::Expression>

Find all descendants of the current input that match the selector in the DescendantSegment

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:



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

def interpret(input, parent, root, path)
  visit(input, parent, path) do |node, parent_of_node, sub_path|
    @next.interpret(node, parent_of_node, root, sub_path)
  end
end

#visit(input, parent, path, &block) ⇒ Object

Visit all descendants of ‘input`. Return results of applying `action` on each.

Parameters:

  • input (Array, Hash)

    the results of processing so far

  • path (Array<String>)

    elements of normalized path to the current input

  • parent (Array, Hash)

    parent of the input object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/janeway/interpreters/descendant_segment_interpreter.rb', line 29

def visit(input, parent, path, &block)
  results = [yield(input, parent, path)]

  case input
  when Array
    results.concat(input.map.with_index { |value, i| visit(value, input, path + [i], &block) })
  when Hash
    results.concat(input.map { |key, value| visit(value, input, path + [key], &block) })
  end
  # basic types are ignored, they will be added by one of the above

  results.flatten(1).compact
end