Class: Janeway::Interpreters::NameSelectorInterpreter

Inherits:
Base
  • Object
show all
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

NameSelectorDeleteIf, NameSelectorDeleter

Constant Summary

Constants inherited from Base

Base::NOTHING

Instance Attribute Summary collapse

Attributes inherited from Base

#next, #node

Instance Method Summary collapse

Methods inherited from Base

#to_s, #type

Constructor Details

#initialize(selector) ⇒ NameSelectorInterpreter

Returns a new instance of NameSelectorInterpreter.

Parameters:



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

#nameObject (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_jsonHash

Return hash representation of this interpreter

Returns:

  • (Hash)


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.

Parameters:

  • input (Array, Hash)

    the results of processing so far

  • _parent (Array, Hash)

    parent of the input object

  • root (Array, Hash)

    the entire input

  • path (Array<String>)

    elements of normalized path to the current 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