Class: Grape::Validations::Types::MultipleTypeCoercer

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/validations/types/multiple_type_coercer.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(types, method = nil) ⇒ MultipleTypeCoercer

Construct a new coercer that will attempt to coerce values to the given list of types in the given order.

Parameters:

  • types (Array<Class>)

    list of allowed types

  • method (#call, #parse) (defaults to: nil)

    method by which values should be coerced. See class docs for default behaviour.


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/grape/validations/types/multiple_type_coercer.rb', line 20

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
    else
      Types.build_coercer type
    end
  end
end

Instance Method Details

#call(value) ⇒ Object, InvalidValue

This method is called from somewhere within +Virtus::Attribute::coerce+ in order to coerce the given value.

Parameters:

  • value (String)

    value to be coerced, in grape this should always be a string.

Returns:


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/grape/validations/types/multiple_type_coercer.rb', line 40

def call(value)
  return @method.call(value) if @method

  @type_coercers.each do |coercer|
    coerced = coercer.coerce(value)

    return coerced if coercer.value_coerced? coerced
  end

  # Declare that we couldn't coerce the value in such a way
  # that Grape won't ask us again if the value is valid
  InvalidValue.new
end

#success?(_primitive, value) ⇒ true, false

This method is called from somewhere within +Virtus::Attribute::value_coerced?+ in order to assert that the value has been coerced successfully. Due to Grape's design this will in fact only be called if a custom coercion method is being used, since #call returns an InvalidValue object if the value could not be coerced.

Parameters:

  • _primitive (Axiom::Types::Type)

    primitive type for the coercion as detected by axiom-types' inference system. For custom types this is typically not much use (i.e. it is +Axiom::Types::Object+) unless special inference rules have been declared for the type.

  • value (Object)

    a coerced result returned from #call

Returns:

  • (true, false)

    whether or not the coerced value satisfies type requirements.


70
71
72
# File 'lib/grape/validations/types/multiple_type_coercer.rb', line 70

def success?(_primitive, value)
  @type_coercers.any? { |coercer| coercer.value_coerced? value }
end