class Grape::Validations::Types::MultipleTypeCoercer

successfully “coerce” the value.
an allowed type it should be declared last, since it will always
to successfully coerce the parameter value. Therefore if String is
:types, and will return the value returned by the first coercer
apply these coercers to parameter values in the order given to
in the array passed to :types using {Types.build_coercer}. It will
MultipleTypeCoercer will build a coercer for each type declared
have been declared to be of variant-type using the :types option.
This class is intended for use with Grape endpoint parameters that

def call(val)

Returns:
  • (Object, InvalidValue) - the coerced result, or an instance

Parameters:
  • val (String) -- value to be coerced, in grape
def call(val)
  # once the value is coerced by the custom method, its type should be checked
  val = @method.call(val) if @method
  coerced_val = InvalidValue.new
  @type_coercers.each do |coercer|
    coerced_val = coercer.call(val)
    return coerced_val unless coerced_val.is_a?(InvalidValue)
  end
  coerced_val
end

def initialize(types, method = nil)

Parameters:
  • method (#call, #parse) -- method by which values should be
  • types (Array) -- list of allowed types
def initialize(types, method = nil)
  @method = method.respond_to?(:parse) ? method.method(:parse) : method
  @type_coercers = types.map do |type|
    if Types.multiple? type
      VariantCollectionCoercer.new type, @method
    else
      Types.build_coercer type, strict: !@method.nil?
    end
  end
end