Class: Janeway::Interpreters::NameSelectorInterpreter
- Defined in:
- lib/janeway/interpreters/name_selector_interpreter.rb
Overview
Interprets a name selector, returns results or forwards them to next selector
Filters the input by returning the key that has the given name.
Must differentiate between a null value of a key that exists (nil) and a key that does not exist ([])
Direct Known Subclasses
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Attributes inherited from Base
Instance Method Summary collapse
-
#as_json ⇒ Hash
Return hash representation of this interpreter.
-
#initialize(selector) ⇒ NameSelectorInterpreter
constructor
A new instance of NameSelectorInterpreter.
-
#interpret(input, _parent, root, path) ⇒ Object
Interpret selector on the given input.
Methods inherited from Base
Constructor Details
#initialize(selector) ⇒ NameSelectorInterpreter
Returns a new instance of NameSelectorInterpreter.
18 19 20 21 |
# File 'lib/janeway/interpreters/name_selector_interpreter.rb', line 18 def initialize(selector) super @name = selector.name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/janeway/interpreters/name_selector_interpreter.rb', line 15 def name @name end |
Instance Method Details
#as_json ⇒ Hash
Return hash representation of this interpreter
40 41 42 |
# File 'lib/janeway/interpreters/name_selector_interpreter.rb', line 40 def as_json { type: type, value: name, next: @next&.as_json } end |
#interpret(input, _parent, root, path) ⇒ Object
Interpret selector on the given input.
28 29 30 31 32 33 34 35 36 |
# File 'lib/janeway/interpreters/name_selector_interpreter.rb', line 28 def interpret(input, _parent, root, path) return [] unless input.is_a?(Hash) && input.key?(name) result = input[name] return [result] unless @next # Forward result to next selector @next.interpret(result, input, root, path + [name]) end |