Class: Janeway::Interpreters::DescendantSegmentInterpreter
- 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
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#interpret(input, parent, root, path) ⇒ Array<AST::Expression>
Find all descendants of the current input that match the selector in the DescendantSegment.
-
#visit(input, parent, path, &block) ⇒ Object
Visit all descendants of ‘input`.
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
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.
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 |