Class: Janeway::Interpreters::IndexSelectorDeleter

Inherits:
IndexSelectorInterpreter show all
Defined in:
lib/janeway/interpreters/index_selector_deleter.rb

Overview

Interprets an index selector, and deletes the matched value.

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

Constructor Details

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

Instance Method Details

#interpret(input, _parent, _root, _path) ⇒ Object

Interpret selector on the given input.



14
15
16
17
18
19
20
21
22
23
# File 'lib/janeway/interpreters/index_selector_deleter.rb', line 14

def interpret(input, _parent, _root, _path)
  return [] unless input.is_a?(Array)

  index = selector.value
  result = input.fetch(index) # raises IndexError if no such index
  input.delete_at(index) # returns nil if deleted value is nil, or if no value was deleted
  [result]
rescue IndexError
  []
end