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



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/jmespath/nodes/function.rb', line 188

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