|
| 1 | +module Grape |
| 2 | + module Validations |
| 3 | + module Types |
| 4 | + # Instances of this class may be passed to |
| 5 | + # +Virtus::Attribute.build+ as the +:coercer+ |
| 6 | + # option, to handle collections of types that |
| 7 | + # provide their own parsing (and optionally, |
| 8 | + # type-checking) functionality. |
| 9 | + # |
| 10 | + # See {CustomTypeCoercer} for details on types |
| 11 | + # that will be supported by this by this coercer. |
| 12 | + # This coercer works in the same way as +CustomTypeCoercer+ |
| 13 | + # except that it expects to receive an array of strings to |
| 14 | + # coerce and will return an array (or optionally, a set) |
| 15 | + # of coerced values. |
| 16 | + # |
| 17 | + # +CustomTypeCoercer+ is already capable of providing type |
| 18 | + # checking for arrays where an independent coercion method |
| 19 | + # is supplied. As such, +CustomTypeCollectionCoercer+ does |
| 20 | + # not allow for such a method to be supplied independently |
| 21 | + # of the type. |
| 22 | + class CustomTypeCollectionCoercer < CustomTypeCoercer |
| 23 | + # A new coercer for collections of the given type. |
| 24 | + # |
| 25 | + # @param type [Class,#parse] |
| 26 | + # type to which items in the array should be coerced. |
| 27 | + # Must implement a +parse+ method which accepts a string, |
| 28 | + # and for the purposes of type-checking it may either be |
| 29 | + # a class, or it may implement a +coerced?+, +parsed?+ or |
| 30 | + # +call+ method (in that order of precedence) which |
| 31 | + # accepts a single argument and returns true if the given |
| 32 | + # array item has been coerced correctly. |
| 33 | + # @param set [Boolean] |
| 34 | + # when true, a +Set+ will be returned by {#call} instead |
| 35 | + # of an +Array+ and duplicate items will be discarded. |
| 36 | + def initialize(type, set = false) |
| 37 | + super(type) |
| 38 | + @set = set |
| 39 | + end |
| 40 | + |
| 41 | + # This method is called from somewhere within |
| 42 | + # +Virtus::Attribute::coerce+ in order to coerce |
| 43 | + # the given value. |
| 44 | + # |
| 45 | + # @param value [Array<String>] an array of values to be coerced |
| 46 | + # @return [Array,Set] the coerced result. May be an +Array+ or a |
| 47 | + # +Set+ depending on the setting given to the constructor |
| 48 | + def call(value) |
| 49 | + coerced = value.map { |item| super(item) } |
| 50 | + |
| 51 | + @set ? Set.new(coerced) : coerced |
| 52 | + end |
| 53 | + |
| 54 | + # This method is called from somewhere within |
| 55 | + # +Virtus::Attribute::value_coerced?+ in order to assert |
| 56 | + # that the all of the values in the array have been coerced |
| 57 | + # successfully. |
| 58 | + # |
| 59 | + # @param primitive [Axiom::Types::Type] primitive type for |
| 60 | + # the coercion as deteced by axiom-types' inference system. |
| 61 | + # @param value [Enumerable] a coerced result returned from {#call} |
| 62 | + # @return [true,false] whether or not all of the coerced values in |
| 63 | + # the collection satisfy type requirements. |
| 64 | + def success?(primitive, value) |
| 65 | + value.is_a?(@set ? Set : Array) && |
| 66 | + value.all? { |item| super(primitive, item) } |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments