Class: Grape::Validations::Types::ArrayCoercer

Inherits:
DryTypeCoercer show all
Defined in:
lib/grape/validations/types/array_coercer.rb

Overview

Coerces elements in an array. It might be an array of strings or integers or an array of arrays of integers.

It could've been possible to use an of method (dry-rb.org/gems/dry-types/1.2/array-with-member/) provided by dry-types. Unfortunately, it doesn't work for Grape because of behavior of Virtus which was used earlier, a `Grape::Validations::Types::PrimitiveCoercer` maintains Virtus behavior in coercing.

Direct Known Subclasses

SetCoercer

Instance Method Summary collapse

Methods inherited from DryTypeCoercer

coercer_instance_for, collection_coercer_for, register_collection

Constructor Details

#initialize(type, strict = false) ⇒ ArrayCoercer

Returns a new instance of ArrayCoercer.


19
20
21
22
23
24
# File 'lib/grape/validations/types/array_coercer.rb', line 19

def initialize(type, strict = false)
  super

  @coercer = scope::Array
  @subtype = type.first
end

Instance Method Details

#call(_val) ⇒ Object


26
27
28
29
30
31
# File 'lib/grape/validations/types/array_coercer.rb', line 26

def call(_val)
  collection = super
  return collection if collection.is_a?(InvalidValue)

  coerce_elements collection
end