Class: JMESPath::Nodes::SortByFunction Private

Inherits:
Function show all
Includes:
TypeChecker
Defined in:
lib/jmespath/nodes/function.rb

This class is part of a private API. You should avoid using this class 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

Constants inherited from Function

Function::FUNCTIONS

Instance Method Summary collapse

Methods included from TypeChecker

#get_type

Methods inherited from Function

create, #initialize, #optimize, #visit

Methods inherited from Node

#chains_with?, #hash_like?, #optimize, #visit

Constructor Details

This class inherits a constructor from JMESPath::Nodes::Function

Instance Method Details

#call(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.



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

def call(args)
  if args.count == 2
    if get_type(args[0]) == ARRAY_TYPE && get_type(args[1]) == EXPRESSION_TYPE
      values = args[0]
      expression = args[1]
      values.sort do |a,b|
        a_value = expression.eval(a)
        b_value = expression.eval(b)
        a_type = get_type(a_value)
        b_type = get_type(b_value)
        if (a_type == STRING_TYPE || a_type == NUMBER_TYPE) && a_type == b_type
          a_value <=> b_value
        else
          return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
        end
      end
    else
      return maybe_raise Errors::InvalidTypeError, "function sort_by() expects an array and an expression"
    end
  else
    return maybe_raise Errors::InvalidArityError, "function sort_by() expects two arguments"
  end
end