Class: InOperator

Inherits:
Object
  • Object
show all
Includes:
BinaryOperator
Defined in:
lib/json_expr/operators/in_operator.rb

Instance Method Summary collapse

Methods included from BinaryOperator

#evaluate

Instance Method Details

#binary(evaluator, haystack, needle) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/json_expr/operators/in_operator.rb', line 8

def binary(evaluator, haystack, needle)
  if haystack.is_a? Array
    haystack.each do |item|
      return true if evaluator.compare(item, needle) == 0
    end
    return false
  elsif haystack.is_a? String
    needle_string = evaluator.string_convert(needle)
    return !needle_string.nil? && haystack.include?(needle_string)
  elsif haystack.is_a?(Hash)
    needle_string = evaluator.string_convert(needle)
    return !needle_string.nil? && haystack.key?(needle_string)
  end
  nil
end