Class: Janeway::Interpreters::NameSelectorDeleteIf
- Inherits:
-
NameSelectorInterpreter
- Object
- Base
- NameSelectorInterpreter
- Janeway::Interpreters::NameSelectorDeleteIf
- 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
Instance Attribute Summary
Attributes inherited from NameSelectorInterpreter
Attributes inherited from Base
Instance Method Summary collapse
-
#as_json ⇒ Hash
Return hash representation of this interpreter.
-
#initialize(selector, &block) ⇒ NameSelectorDeleteIf
constructor
A new instance of NameSelectorDeleteIf.
-
#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
Constructor Details
#initialize(selector, &block) ⇒ NameSelectorDeleteIf
Returns a new instance of NameSelectorDeleteIf.
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_json ⇒ Hash
Return hash representation of this interpreter
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.
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 |