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 anything else.

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.

Instance Method Summary collapse

Constructor Details

#initialize(type, strict = false) ⇒ ArrayCoercer

Returns a new instance of ArrayCoercer.



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

def initialize(type, strict = false)
  super

  @coercer = scope::Array
  @elem_coercer = PrimitiveCoercer.new(type.first, strict)
end

Instance Method Details

#call(_val) ⇒ Object



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

def call(_val)
  collection = super

  return collection if collection.is_a?(InvalidValue)

  coerce_elements collection
end