Class: Janeway::Interpreters::WildcardSelectorDeleteIf

Inherits:
WildcardSelectorInterpreter show all
Includes:
IterationHelper
Defined in:
lib/janeway/interpreters/wildcard_selector_delete_if.rb

Overview

Interprets a wildcard selector by deleting array / hash values for which a block returns true.

Constant Summary

Constants included from IterationHelper

IterationHelper::PASS_ALL

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 WildcardSelectorInterpreter

#interpret_array, #interpret_hash

Methods inherited from Base

#as_json, #selector, #to_s, #type

Constructor Details

#initialize(selector, &block) ⇒ WildcardSelectorDeleteIf

Returns a new instance of WildcardSelectorDeleteIf.

Parameters:



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

Delete all elements from the 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

Returns:

  • (Array)

    deleted elements



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

def interpret(input, _parent, _root, path)
  case input
  when Array then maybe_delete_array_values(input, path)
  when Hash then maybe_delete_hash_values(input, path)
  else []
  end
end

#maybe_delete_array_values(input, path) ⇒ Object

Parameters:

  • input (Array)
  • path (Array)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/janeway/interpreters/wildcard_selector_delete_if.rb', line 38

def maybe_delete_array_values(input, path)
  # Fast path: unconditional delete (from Interpreter#make_deleter).
  # Bulk clear is O(n) with much smaller constants than reverse-iterate
  # + delete_at + per-element proc call.
  if @block.equal?(IterationHelper::PASS_ALL)
    results = input.dup
    input.clear
    return results
  end

  results = []
  (input.size - 1).downto(0).each do |i|
    next unless @yield_proc.yield(input[i], input, path + [i])

    results << input.delete_at(i)
  end
  results.reverse
end

#maybe_delete_hash_values(input, path) ⇒ Object

Parameters:

  • input (Hash)
  • path (Array)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/janeway/interpreters/wildcard_selector_delete_if.rb', line 59

def maybe_delete_hash_values(input, path)
  # Fast path: unconditional delete (from Interpreter#make_deleter).
  if @block.equal?(IterationHelper::PASS_ALL)
    results = input.values
    input.clear
    return results
  end

  results = []
  input.each do |key, value|
    next unless @yield_proc.yield(value, input, path + [key])

    results << input.delete(key)
  end
  results
end