Class: JMESPath::Nodes::SortFunction 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



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/jmespath/nodes/function.rb', line 436

def call(args)
  if args.count == 1
    value = args.first
    if value.respond_to?(:to_ary)
      value = value.to_ary
      # every element in the list must be of the same type
      array_type = get_type(value[0])
      if array_type == STRING_TYPE || array_type == NUMBER_TYPE || value.empty?
        # stable sort
        n = 0
        value.sort_by do |v|
          value_type = get_type(v)
          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
          [v, 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() expects values to be an array of numbers or integers'
    end
  else
    return maybe_raise Errors::InvalidArityError, 'function sort() expects one argument'
  end
end