Class: Janeway::Interpreters::IndexSelectorDeleteIf

Inherits:
IndexSelectorInterpreter show all
Includes:
IterationHelper
Defined in:
lib/janeway/interpreters/index_selector_delete_if.rb

Overview

Interprets an index selector, and deletes the matched value if the block returns true.

Constant Summary

Constants inherited from Base

Base::NOTHING

Instance Attribute Summary

Attributes inherited from Base

#next, #node

Instance Method Summary collapse

Methods included from IterationHelper

#make_yield_proc, #normalized_path

Methods inherited from Base

#as_json, #selector, #to_s, #type

Constructor Details

#initialize(selector, &block) ⇒ IndexSelectorDeleteIf

Returns a new instance of IndexSelectorDeleteIf.

Parameters:



13
14
15
16
17
18
19
# File 'lib/janeway/interpreters/index_selector_delete_if.rb', line 13

def initialize(selector, &block)
  super(selector)
  @block = block

  # Make a proc that yields the correct number of values to a block
  @yield_proc = make_yield_proc(&block)
end

Instance Method Details

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

Interpret selector on the given input.

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



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

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
  return unless @yield_proc.call(input[index], input, path + [index])

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