Sha256: 5478678248e1d7265a94300daebb5acc5e841728832bb049c0f71b9ab7650695

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

module JMESPath
  # @api private
  module Nodes
    class Comparator < Node
      attr_reader :left, :right

      def initialize(left, right)
        @left = left
        @right = right
      end

      def self.create(relation, left, right)
        type = begin
          case relation
          when '==' then EqComparator
          when '!=' then NeqComparator
          when '>' then GtComparator
          when '>=' then GteComparator
          when '<' then LtComparator
          when '<=' then LteComparator
          end
        end
        type.new(left, right)
      end

      def visit(value)
        check(@left.visit(value), @right.visit(value))
      end

      def optimize
        self.class.new(@left.optimize, @right.optimize)
      end

      private

      def check(left_value, right_value)
        nil
      end
    end

    class EqComparator < Comparator
      def check(left_value, right_value)
        left_value == right_value
      end
    end

    class NeqComparator < Comparator
      def check(left_value, right_value)
        left_value != right_value
      end
    end

    class GtComparator < Comparator
      def check(left_value, right_value)
        left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value > right_value
      end
    end

    class GteComparator < Comparator
      def check(left_value, right_value)
        left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value >= right_value
      end
    end

    class LtComparator < Comparator
      def check(left_value, right_value)
        left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value < right_value
      end
    end

    class LteComparator < Comparator
      def check(left_value, right_value)
        left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value <= right_value
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
burtpath-1.1.2 lib/jmespath/nodes/comparator.rb
burtpath-1.1.1 lib/jmespath/nodes/comparator.rb
jmespath-1.1.1 lib/jmespath/nodes/comparator.rb
jmespath-1.1.0 lib/jmespath/nodes/comparator.rb
burtpath-1.1.0 lib/jmespath/nodes/comparator.rb