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.



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/jmespath/nodes/function.rb', line 447

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"
        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}"
          raise Errors::InvalidTypeError, msg
        end
        value
      end
    else
      msg = "function #{mode}() expects an array and an expression"
      raise Errors::InvalidTypeError, msg
    end
  else
    msg = "function #{mode}() expects two arguments"
    raise Errors::InvalidArityError, msg
  end
end