Class: Janeway::Interpreters::IndexSelectorInterpreter

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

Overview

Interprets an index selector, returns results or forwards them to next selector

Filter the input by returning the array element with the given index. Return empty list if input is not an array, or if array does not contain index.

Output is an array because all selectors must return node lists, even if they only select a single element.

Returns:

  • (Array)

Direct Known Subclasses

IndexSelectorDeleter

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

Interpret an index selector on the given input. NOTHING is selected, and it is not an error, if the index lies outside the range of the array. NOTHING is selected from a value that is not an array.

Parameters:

  • input (Array, Hash)

    the results of processing so far

  • root (Array, Hash)

    the entire input

  • path (Array<String>)

    elements of normalized path to the current input



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

def interpret(input, _parent, root, path)
  return [] unless input.is_a?(Array)

  result = input.fetch(selector.value) # raises IndexError if no such index
  return [result] unless @next

  @next.interpret(result, input, root, path + [selector.value])
rescue IndexError
  []
end