Class: Janeway::Interpreters::NameSelectorDeleteIf

Inherits:
NameSelectorInterpreter show all
Includes:
IterationHelper
Defined in:
lib/janeway/interpreters/name_selector_delete_if.rb

Overview

Interprets a name selector, and deletes the matched values if the block returns a truthy value.

Constant Summary

Constants inherited from Base

Base::NOTHING

Instance Attribute Summary

Attributes inherited from NameSelectorInterpreter

#name

Attributes inherited from Base

#next, #node

Instance Method Summary collapse

Methods included from IterationHelper

#make_yield_proc, #normalized_path

Methods inherited from Base

#selector, #to_s, #type

Constructor Details

#initialize(selector, &block) ⇒ NameSelectorDeleteIf

Returns a new instance of NameSelectorDeleteIf.

Parameters:



13
14
15
16
17
18
19
# File 'lib/janeway/interpreters/name_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

#as_jsonHash

Return hash representation of this interpreter

Returns:

  • (Hash)


37
38
39
# File 'lib/janeway/interpreters/name_selector_delete_if.rb', line 37

def as_json
  { type: type, value: name }
end

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

Interpret selector on the given input. Delete matching value for which the block returns truthy value.

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)

    elements of normalized path to the current input



28
29
30
31
32
33
# File 'lib/janeway/interpreters/name_selector_delete_if.rb', line 28

def interpret(input, _parent, _root, path)
  return [] unless input.is_a?(Hash) && input.key?(name)
  return [] unless @yield_proc.call(input[name], input, path + [name])

  [input.delete(name)]
end