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
- MAPPING =
{ Grape::API::Boolean => DryTypes::Params::Bool, BigDecimal => DryTypes::Params::Decimal, # unfortunately, a +Params+ scope doesn't contain String String => DryTypes::Coercible::String }.freeze
- STRICT_MAPPING =
{ Grape::API::Boolean => DryTypes::Strict::Bool, BigDecimal => DryTypes::Strict::Decimal }.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, register_collection
Constructor Details
#initialize(type, strict = false) ⇒ PrimitiveCoercer
Returns a new instance of PrimitiveCoercer.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/grape/validations/types/primitive_coercer.rb', line 25 def initialize(type, strict = false) super @type = type @coercer = if strict STRICT_MAPPING.fetch(type) { scope.const_get(type.name) } else MAPPING.fetch(type) { scope.const_get(type.name) } end end |
Instance Method Details
#call(val) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/grape/validations/types/primitive_coercer.rb', line 37 def call(val) return InvalidValue.new if reject?(val) return nil if val.nil? || treat_as_nil?(val) super end |