Class: Grape::Validations::Types::PrimitiveCoercer
- Inherits:
-
DryTypeCoercer
- Object
- DryTypeCoercer
- Grape::Validations::Types::PrimitiveCoercer
- 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.
Constant Summary collapse
- REJECTED_INPUTS =
The input classes Virtus refused for a given declared type, keyed by that type. Resolved once in #initialize rather than re-derived from
typeon every value: the answer only ever depends on the declaration, and every coerced attribute of every request asks. { String => [Array, Hash].freeze, Hash => [String].freeze }.freeze
Instance Method Summary collapse
- #call(val) ⇒ Object
-
#initialize(type, strict: false) ⇒ PrimitiveCoercer
constructor
A new instance of PrimitiveCoercer.
Methods inherited from DryTypeCoercer
coercer_instance_for, collection_coercer_for
Methods included from Util::FreezeOnNew
Constructor Details
#initialize(type, strict: false) ⇒ PrimitiveCoercer
Returns a new instance of PrimitiveCoercer.
16 17 18 19 20 21 22 |
# File 'lib/grape/validations/types/primitive_coercer.rb', line 16 def initialize(type, strict: false) super @coercer = cache_coercer[type] @rejected_inputs = REJECTED_INPUTS[type] @treat_empty_as_nil = type != String end |
Instance Method Details
#call(val) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/grape/validations/types/primitive_coercer.rb', line 24 def call(val) return InvalidValue.new if reject?(val) return if val.nil? || treat_as_nil?(val) super end |