Class: Grape::Validations::Types::CustomTypeCollectionCoercer
- Inherits:
-
CustomTypeCoercer
- Object
- CustomTypeCoercer
- Grape::Validations::Types::CustomTypeCollectionCoercer
- Defined in:
- lib/grape/validations/types/custom_type_collection_coercer.rb
Overview
Instances of this class may be passed to Virtus::Attribute.build
as the :coercer
option, to handle collections of types that provide their own parsing (and optionally, type-checking) functionality.
See CustomTypeCoercer for details on types that will be supported by this by this coercer. This coercer works in the same way as CustomTypeCoercer
except that it expects to receive an array of strings to coerce and will return an array (or optionally, a set) of coerced values.
CustomTypeCoercer
is already capable of providing type checking for arrays where an independent coercion method is supplied. As such, CustomTypeCollectionCoercer
does not allow for such a method to be supplied independently of the type.
Instance Method Summary collapse
-
#call(value) ⇒ Array, Set
This method is called from somewhere within
Virtus::Attribute::coerce
in order to coerce the given value. -
#initialize(type, set = false) ⇒ CustomTypeCollectionCoercer
constructor
A new coercer for collections of the given type.
-
#success?(primitive, value) ⇒ true, false
This method is called from somewhere within
Virtus::Attribute::value_coerced?
in order to assert that the all of the values in the array have been coerced successfully.
Methods inherited from CustomTypeCoercer
Constructor Details
#initialize(type, set = false) ⇒ CustomTypeCollectionCoercer
A new coercer for collections of the given type.
36 37 38 39 |
# File 'lib/grape/validations/types/custom_type_collection_coercer.rb', line 36 def initialize(type, set = false) super(type) @set = set end |
Instance Method Details
#call(value) ⇒ Array, Set
This method is called from somewhere within Virtus::Attribute::coerce
in order to coerce the given value.
48 49 50 51 52 |
# File 'lib/grape/validations/types/custom_type_collection_coercer.rb', line 48 def call(value) coerced = value.map { |item| super(item) } @set ? Set.new(coerced) : coerced end |
#success?(primitive, value) ⇒ true, false
This method is called from somewhere within Virtus::Attribute::value_coerced?
in order to assert that the all of the values in the array have been coerced successfully.
64 65 66 67 |
# File 'lib/grape/validations/types/custom_type_collection_coercer.rb', line 64 def success?(primitive, value) value.is_a?(@set ? Set : Array) && value.all? { |item| super(primitive, item) } end |