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

#initialize

Constructor Details

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

Instance Method Details

#interpret(input, root) ⇒ 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

  • root (Array, Hash)

    the entire input

Returns:



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

def interpret(input, root)
  visit(input) do |node|
    @next.interpret(node, root)
  end
end

#visit(input, &action) ⇒ Object

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



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

def visit(input, &action)
  results = [yield(input)]

  case input
  when Array
    results.concat(input.map { |elt| visit(elt, &action) })
  when Hash
    results.concat(input.values.map { |value| visit(value, &action) })
  else
    input
  end

  results.flatten(1).compact
end