Class: JMESPath::Nodes::SortByFunction Private

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

Overview

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.

API:

  • private

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?, #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.

API:

  • private



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/jmespath/nodes/function.rb', line 472

def call(args)
  if args.count == 2
    if get_type(args[0]) == ARRAY_TYPE && get_type(args[1]) == EXPRESSION_TYPE
      values = args[0].to_ary
      expression = args[1]
      array_type = get_type(expression.eval(values[0]))
      if array_type == STRING_TYPE || array_type == NUMBER_TYPE || values.empty?
        # stable sort the list
        n = 0
        values.sort_by do |value|
          value = expression.eval(value)
          value_type = get_type(value)
          if value_type != array_type
            msg = 'function sort() expects values to be an array of only numbers, or only integers'
            return maybe_raise Errors::InvalidTypeError, msg
          end
          n += 1
          [value, n]
        end
      else
        return maybe_raise Errors::InvalidTypeError, 'function sort() expects values to be an array of numbers or integers'
      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