Class: Janeway::Interpreters::FilterSelectorDeleteIf
- Inherits:
-
FilterSelectorInterpreter
- Object
- Base
- FilterSelectorInterpreter
- Janeway::Interpreters::FilterSelectorDeleteIf
- Includes:
- IterationHelper
- Defined in:
- lib/janeway/interpreters/filter_selector_delete_if.rb
Overview
Interprets a filter selector by deleting matching values for which the block returns a truthy value
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(selector, &block) ⇒ FilterSelectorDeleteIf
constructor
A new instance of FilterSelectorDeleteIf.
-
#interpret_array(input, root, path) ⇒ Object
Interpret selector on the input.
-
#interpret_hash(input, root, path) ⇒ Object
Interpret selector on the input.
Methods included from IterationHelper
#make_yield_proc, #normalized_path
Methods inherited from FilterSelectorInterpreter
#as_json, #interpret, setup_interpreter_tree
Methods inherited from Base
#as_json, #interpret, #selector, #to_s, #type
Constructor Details
#initialize(selector, &block) ⇒ FilterSelectorDeleteIf
Returns a new instance of FilterSelectorDeleteIf.
13 14 15 16 17 18 19 |
# File 'lib/janeway/interpreters/filter_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_array(input, root, path) ⇒ Object
Interpret selector on the input.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/janeway/interpreters/filter_selector_delete_if.rb', line 48 def interpret_array(input, root, path) # Apply filter expressions to the input data results = [] # Iterate in reverse order so that deletion does not alter the remaining indexes i = input.size input.reverse_each do |value| i -= 1 # calculate array index # Run filter and interpret result result = @expr.interpret(value, nil, root, []) case result when FalseClass then next # comparison test - fail when Array then next if result.empty? end # filter test passed, next yield value to block next unless @yield_proc.call(value, input, path + [i]) results << input.delete_at(i) end results.reverse end |
#interpret_hash(input, root, path) ⇒ Object
Interpret selector on the input.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/janeway/interpreters/filter_selector_delete_if.rb', line 25 def interpret_hash(input, root, path) # Apply filter expressions to the input data results = [] input.each do |key, value| # Run filter and interpret result result = @expr.interpret(value, nil, root, []) case result when FalseClass then next # comparison test - fail when Array then next if result.empty? end # filter test passed, next yield value to block next unless @yield_proc.call(value, input, path + [key]) results << input.delete(key) end results end |