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.



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/jmespath/nodes/function.rb', line 506

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
      values = values.to_ary
      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 = String.new("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