Class: Janeway::Interpreters::IndexSelectorDeleteIf
- Inherits:
-
IndexSelectorInterpreter
- Object
- Base
- IndexSelectorInterpreter
- Janeway::Interpreters::IndexSelectorDeleteIf
- 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
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(selector, &block) ⇒ IndexSelectorDeleteIf
constructor
A new instance of IndexSelectorDeleteIf.
-
#interpret(input, _parent, _root, path) ⇒ Object
Interpret selector on the given input.
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.
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.
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 |