Class: Janeway::Interpreters::FilterSelectorInterpreter
- Defined in:
- lib/janeway/interpreters/filter_selector_interpreter.rb
Overview
Interprets a filter selector, returns results or forwards them to next selector
Direct Known Subclasses
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.setup_interpreter_tree(selector) ⇒ Interpreters::Base
FIXME: should this be combined with similar func in Interpreter? FIXME: move this to a separate module?.
Instance Method Summary collapse
- #as_json ⇒ Hash
-
#initialize(selector) ⇒ FilterSelectorInterpreter
constructor
Set up the internal interpreter chain for the FilterSelector.
-
#interpret(input, _parent, root, path) ⇒ Object
Interpret selector on the input.
-
#interpret_array(input, root, path) ⇒ Object
Interpret selector on the input.
-
#interpret_hash(input, root, path) ⇒ Object
Interpret selector on the input.
Methods inherited from Base
Constructor Details
#initialize(selector) ⇒ FilterSelectorInterpreter
Set up the internal interpreter chain for the FilterSelector.
13 14 15 16 |
# File 'lib/janeway/interpreters/filter_selector_interpreter.rb', line 13 def initialize(selector) super @expr = self.class.setup_interpreter_tree(selector) end |
Class Method Details
.setup_interpreter_tree(selector) ⇒ Interpreters::Base
FIXME: should this be combined with similar func in Interpreter? FIXME: move this to a separate module?
Set up a tree of interpreters which can process input for the filter expression. For a jsonpath query like '$.a[?@.b == $.x]', this sets up interpreters for '@.b == $.x'.
24 25 26 |
# File 'lib/janeway/interpreters/filter_selector_interpreter.rb', line 24 def self.setup_interpreter_tree(selector) TreeConstructor.ast_node_to_interpreter(selector.value) end |
Instance Method Details
#as_json ⇒ Hash
90 91 92 |
# File 'lib/janeway/interpreters/filter_selector_interpreter.rb', line 90 def as_json { type: type, value: node.to_s, next: @next&.as_json }.compact end |
#interpret(input, _parent, root, path) ⇒ Object
Interpret selector on the input.
33 34 35 36 37 38 39 |
# File 'lib/janeway/interpreters/filter_selector_interpreter.rb', line 33 def interpret(input, _parent, root, path) case input when Array then interpret_array(input, root, path) when Hash then interpret_hash(input, root, path) else [] # early exit end end |
#interpret_array(input, root, path) ⇒ Object
Interpret selector on the input.
55 56 57 58 59 |
# File 'lib/janeway/interpreters/filter_selector_interpreter.rb', line 55 def interpret_array(input, root, path) node_list = [] input.each_with_index { |value, i| node_list << [i, value] if filter_match?(value, root) } forward_matches(node_list, input, root, path) end |
#interpret_hash(input, root, path) ⇒ Object
Interpret selector on the input.
45 46 47 48 49 |
# File 'lib/janeway/interpreters/filter_selector_interpreter.rb', line 45 def interpret_hash(input, root, path) node_list = [] input.each { |key, value| node_list << [key, value] if filter_match?(value, root) } forward_matches(node_list, input, root, path) end |