Module: JMESPath::Nodes::CompareBy Private

Includes:
TypeChecker
Included in:
MaxByFunction, MinByFunction
Defined in:
lib/jmespath/nodes/function.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constant Summary

Constants included from TypeChecker

TypeChecker::ARRAY_TYPE, TypeChecker::BOOLEAN_TYPE, TypeChecker::EXPRESSION_TYPE, TypeChecker::NULL_TYPE, TypeChecker::NUMBER_TYPE, TypeChecker::OBJECT_TYPE, TypeChecker::STRING_TYPE, TypeChecker::TYPE_NAMES

Instance Method Summary collapse

Methods included from TypeChecker

#get_type

Instance Method Details

#compare_by(mode, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/jmespath/nodes/function.rb', line 492

def compare_by(mode, *args)
  if args.count == 2
    values = args[0]
    expression = args[1]
    if get_type(values) == ARRAY_TYPE && get_type(expression) == EXPRESSION_TYPE
      type = get_type(expression.eval(values.first))
      if type != NUMBER_TYPE && type != STRING_TYPE
        msg = "function #{mode}() expects values to be strings or numbers"
        return maybe_raise Errors::InvalidTypeError, msg
      end
      values.send(mode) do |entry|
        value = expression.eval(entry)
        value_type = get_type(value)
        if value_type != type
          msg = "function #{mode}() encountered a type mismatch in "
          msg << "sequence: #{type}, #{value_type}"
          return maybe_raise Errors::InvalidTypeError, msg
        end
        value
      end
    else
      msg = "function #{mode}() expects an array and an expression"
      return maybe_raise Errors::InvalidTypeError, msg
    end
  else
    msg = "function #{mode}() expects two arguments"
    return maybe_raise Errors::InvalidArityError, msg
  end
end