|
| 1 | +module Grape |
| 2 | + module Validations |
| 3 | + module Types |
| 4 | + # This class is intended for use with Grape endpoint parameters that |
| 5 | + # have been declared to be of variant-type using the +:types+ option. |
| 6 | + # +MultipleTypeCoercer+ will build a coercer for each type declared |
| 7 | + # in the array passed to +:types+ using {Types.build_coercer}. It will |
| 8 | + # apply these coercers to parameter values in the order given to |
| 9 | + # +:types+, and will return the value returned by the first coercer |
| 10 | + # to successfully coerce the parameter value. Therefore if +String+ is |
| 11 | + # an allowed type it should be declared last, since it will always |
| 12 | + # successfully "coerce" the value. |
| 13 | + class MultipleTypeCoercer |
| 14 | + # Construct a new coercer that will attempt to coerce |
| 15 | + # values to the given list of types in the given order. |
| 16 | + # |
| 17 | + # @param types [Array<Class>] list of allowed types |
| 18 | + # @param method [#call,#parse] method by which values should be |
| 19 | + # coerced. See class docs for default behaviour. |
| 20 | + def initialize(types, method = nil) |
| 21 | + @method = method.respond_to?(:parse) ? method.method(:parse) : method |
| 22 | + |
| 23 | + @type_coercers = types.map do |type| |
| 24 | + if Types.multiple? type |
| 25 | + VariantCollectionCoercer.new type |
| 26 | + else |
| 27 | + Types.build_coercer type |
| 28 | + end |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + # This method is called from somewhere within |
| 33 | + # +Virtus::Attribute::coerce+ in order to coerce |
| 34 | + # the given value. |
| 35 | + # |
| 36 | + # @param value [String] value to be coerced, in grape |
| 37 | + # this should always be a string. |
| 38 | + # @return [Object,InvalidValue] the coerced result, or an instance |
| 39 | + # of {InvalidValue} if the value could not be coerced. |
| 40 | + def call(value) |
| 41 | + return @method.call(value) if @method |
| 42 | + |
| 43 | + @type_coercers.each do |coercer| |
| 44 | + coerced = coercer.coerce(value) |
| 45 | + |
| 46 | + return coerced if coercer.value_coerced? coerced |
| 47 | + end |
| 48 | + |
| 49 | + # Declare that we couldn't coerce the value in such a way |
| 50 | + # that Grape won't ask us again if the value is valid |
| 51 | + InvalidValue.new |
| 52 | + end |
| 53 | + |
| 54 | + # This method is called from somewhere within |
| 55 | + # +Virtus::Attribute::value_coerced?+ in order to |
| 56 | + # assert that the value has been coerced successfully. |
| 57 | + # Due to Grape's design this will in fact only be called |
| 58 | + # if a custom coercion method is being used, since {#call} |
| 59 | + # returns an {InvalidValue} object if the value could not |
| 60 | + # be coerced. |
| 61 | + # |
| 62 | + # @param _primitive [Axiom::Types::Type] primitive type |
| 63 | + # for the coercion as detected by axiom-types' inference |
| 64 | + # system. For custom types this is typically not much use |
| 65 | + # (i.e. it is +Axiom::Types::Object+) unless special |
| 66 | + # inference rules have been declared for the type. |
| 67 | + # @param value [Object] a coerced result returned from {#call} |
| 68 | + # @return [true,false] whether or not the coerced value |
| 69 | + # satisfies type requirements. |
| 70 | + def success?(_primitive, value) |
| 71 | + @type_coercers.any? { |coercer| coercer.value_coerced? value } |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | +end |
0 commit comments