Class: JMESPath::Nodes::MinFunction 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?, #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.



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/jmespath/nodes/function.rb', line 266

def call(args)
  if args.count == 1
    values = args.first
  else
    return maybe_raise Errors::InvalidArityError, 'function min() expects one argument'
  end
  if values.respond_to?(:to_ary)
    values = values.to_ary
    return nil if values.empty?
    first = values.first
    first_type = get_type(first)
    unless first_type == NUMBER_TYPE || first_type == STRING_TYPE
      msg = String.new('function min() expects numeric or string values')
      return maybe_raise Errors::InvalidTypeError, msg
    end
    values.inject([first, first_type]) do |(min, min_type), v|
      v_type = get_type(v)
      if min_type == v_type
        v < min ? [v, v_type] : [min, min_type]
      else
        msg = String.new('function min() encountered a type mismatch in sequence: ')
        msg << "#{min_type}, #{v_type}"
        return maybe_raise Errors::InvalidTypeError, msg
      end
    end.first
  else
    return maybe_raise Errors::InvalidTypeError, 'function min() expects an array'
  end
end