Class: Janeway::Interpreters::FilterSelectorInterpreter

Inherits:
Base
  • Object
show all
Defined in:
lib/janeway/interpreters/filter_selector_interpreter.rb

Overview

Interprets a filter selector, returns results or forwards them to next selector

Constant Summary

Constants inherited from Base

Base::NOTHING

Instance Attribute Summary

Attributes inherited from Base

#next, #node

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selector) ⇒ FilterSelectorInterpreter

Set up the internal interpreter chain for the FilterSelector.

Parameters:



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[[email protected] == $.x]’, this sets up interpreters for ‘@.b == $.x’.

Returns:



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

#interpret(input, root) ⇒ Object

Interpret selector on the input.

Parameters:

  • input (Array, Hash)

    the results of processing so far

  • root (Array, Hash)

    the entire input



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/janeway/interpreters/filter_selector_interpreter.rb', line 31

def interpret(input, root)
  values =
    case input
    when Array then input
    when Hash then input.values
    else return [] # early exit
    end

  # Apply filter expressions to the input data
  node_list = []
  values.each do |value|
    # Run filter and interpret result
    result = @expr.interpret(value, root)
    case result
    when TrueClass then node_list << value # comparison test - pass
    when FalseClass then nil # comparison test - fail
    when Array then node_list << value unless result.empty? # existence test - node list
    else
      node_list << value # existence test. Null values here == success.
    end
  end
  return node_list unless @next

  # Apply child selector to each node in the output node list
  results = []
  node_list.each do |node|
    results.concat @next.interpret(node, root)
  end
  results
end