Class: Janeway::Interpreters::WildcardSelectorDeleteIf
- Inherits:
-
WildcardSelectorInterpreter
- Object
- Base
- WildcardSelectorInterpreter
- Janeway::Interpreters::WildcardSelectorDeleteIf
- 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 inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(selector, &block) ⇒ WildcardSelectorDeleteIf
constructor
A new instance of WildcardSelectorDeleteIf.
-
#interpret(input, _parent, _root, path) ⇒ Array
Delete all elements from the input.
- #maybe_delete_array_values(input, path) ⇒ Object
- #maybe_delete_hash_values(input, path) ⇒ Object
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
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
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
38 39 40 41 42 43 44 45 46 |
# File 'lib/janeway/interpreters/wildcard_selector_delete_if.rb', line 38 def maybe_delete_array_values(input, path) 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
50 51 52 53 54 55 56 57 58 |
# File 'lib/janeway/interpreters/wildcard_selector_delete_if.rb', line 50 def maybe_delete_hash_values(input, path) results = [] input.each do |key, value| next unless @yield_proc.yield(value, input, path + [key]) results << input.delete(key) end results end |