Class: Grape::Validations::Types::CustomTypeCollectionCoercer

Inherits:
CustomTypeCoercer show all
Defined in:
lib/grape/validations/types/custom_type_collection_coercer.rb

Overview

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

Methods inherited from CustomTypeCoercer

#coerced?

Constructor Details

#initialize(type, set = false) ⇒ CustomTypeCollectionCoercer

A new coercer for collections of the given type.

Parameters:

  • type (Class, #parse)

    type to which items in the array should be coerced. Must implement a parse method which accepts a string, and for the purposes of type-checking it may either be a class, or it may implement a coerced?, parsed? or call method (in that order of precedence) which accepts a single argument and returns true if the given array item has been coerced correctly.

  • set (Boolean) (defaults to: false)

    when true, a Set will be returned by #call instead of an Array and duplicate items will be discarded.



32
33
34
35
# File 'lib/grape/validations/types/custom_type_collection_coercer.rb', line 32

def initialize(type, set = false)
  super(type)
  @set = set
end

Instance Method Details

#call(value) ⇒ Array, Set

Coerces the given value.

Parameters:

  • value (Array<String>)

    an array of values to be coerced

Returns:

  • (Array, Set)

    the coerced result. May be an Array or a Set depending on the setting given to the constructor



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/grape/validations/types/custom_type_collection_coercer.rb', line 42

def call(value)
  coerced = value.map do |item|
    coerced_item = super(item)

    return coerced_item if coerced_item.is_a?(InvalidValue)

    coerced_item
  end

  @set ? Set.new(coerced) : coerced
end