Class: Janeway::Interpreters::WildcardSelectorInterpreter

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

Overview

Interprets a wildcard selector, returns results or forwards them to next selector

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, #to_s, #type

Constructor Details

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

Instance Method Details

#interpret(input, _parent, root, path) ⇒ Object

Return values from the input. For array, return the array. For Hash, return hash values. For anything else, return empty list.

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



20
21
22
23
24
25
26
# File 'lib/janeway/interpreters/wildcard_selector_interpreter.rb', line 20

def interpret(input, _parent, root, path)
  case input
  when Array then interpret_array(input, root, path)
  when Hash then interpret_hash(input, root, path)
  else []
  end
end

#interpret_array(input, root, path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/janeway/interpreters/wildcard_selector_interpreter.rb', line 40

def interpret_array(input, root, path)
  return input if input.empty? # early exit, no need for further processing on empty list
  return input unless @next

  # Apply child selector to each node in the output node list
  results = []
  input.each_with_index do |value, i|
    results.concat @next.interpret(value, input, root, path + [i])
  end
  results
end

#interpret_hash(input, root, path) ⇒ Object



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

def interpret_hash(input, root, path)
  return [] if input.empty? # early exit, no need for further processing on empty list
  return input.values unless @next

  # Apply child selector to each node in the output node list
  results = []
  input.each do |key, value|
    results.concat @next.interpret(value, input, root, path + [key])
  end
  results
end