Class: Janeway::Interpreters::UnaryOperatorInterpreter

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

Overview

Interprets the “not” operator within a filter selector logical expression. The only other unary operator is “minus”, which is consumed during parsing and is not part of the AST.

Constant Summary

Constants inherited from Base

Base::NOTHING

Instance Attribute Summary

Attributes inherited from Base

#next, #node

Instance Method Summary collapse

Methods inherited from Base

#as_json, #selector, #to_s, #type

Constructor Details

#initialize(operator) ⇒ UnaryOperatorInterpreter

Returns a new instance of UnaryOperatorInterpreter.



12
13
14
15
16
17
18
# File 'lib/janeway/interpreters/unary_operator_interpreter.rb', line 12

def initialize(operator)
  super
  raise "Unknown unary operator: #{name}" unless operator.name == :not

  # Expression which must be evaluated, not operator will be applied to result
  @operand = TreeConstructor.ast_node_to_interpreter(operator.operand)
end

Instance Method Details

#interpret(input, parent, root, _path) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/janeway/interpreters/unary_operator_interpreter.rb', line 21

def interpret(input, parent, root, _path)
  result = @operand.interpret(input, parent, root, [])
  case result
  when Array then result.empty?
  when TrueClass, FalseClass then !result
  else
    raise "don't know how to apply not operator to #{result.inspect}"
  end
end