Class: Grape::Validations::Types::PrimitiveCoercer

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

Overview

Coerces the given value to a type defined via a type argument during initialization. When strict is true, it doesn’t coerce a value but check that it has the proper type.

Instance Method Summary collapse

Methods inherited from DryTypeCoercer

coercer_instance_for, collection_coercer_for

Constructor Details

#initialize(type, strict = false) ⇒ PrimitiveCoercer

Returns a new instance of PrimitiveCoercer.



10
11
12
13
14
# File 'lib/grape/validations/types/primitive_coercer.rb', line 10

def initialize(type, strict = false)
  super

  @coercer = cache_coercer[type]
end

Instance Method Details

#call(val) ⇒ Object



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

def call(val)
  return InvalidValue.new if reject?(val)
  return nil if val.nil? || treat_as_nil?(val)

  super
end