Class: Janeway::Interpreters::ArraySliceSelectorInterpreter
- Defined in:
- lib/janeway/interpreters/array_slice_selector_interpreter.rb
Overview
Interprets array slice selector on the given input
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#interpret(input, root) ⇒ Array
Filter the input by applying the array slice selector.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Janeway::Interpreters::Base
Instance Method Details
#interpret(input, root) ⇒ Array
Filter the input by applying the array slice selector.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/janeway/interpreters/array_slice_selector_interpreter.rb', line 17 def interpret(input, root) return [] unless input.is_a?(Array) return [] if selector&.step&.zero? # RFC: When step is 0, no elements are selected. # Calculate the upper and lower indices of the target range lower = selector.lower_index(input.size) upper = selector.upper_index(input.size) # Collect values from target indices. Omit the value from the final index. results = if selector.step.positive? lower.step(to: upper - 1, by: selector.step).map { input[_1] } else upper.step(to: lower + 1, by: selector.step).map { input[_1] } end return results unless @next # Apply child selector to each node in the output node list node_list = results results = [] node_list.each do |node| results.concat @next.interpret(node, root) end results end |