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.
Constant Summary collapse
- MAPPING =
{ Grape::API::Boolean => DryTypes::Params::Bool, # unfortunatelly, a +Params+ scope doesn't contain String String => DryTypes::Coercible::String }.freeze
- STRICT_MAPPING =
{ Grape::API::Boolean => DryTypes::Strict::Bool }.freeze
Instance Method Summary collapse
- #call(val) ⇒ Object
-
#initialize(type, strict = false) ⇒ PrimitiveCoercer
constructor
A new instance of PrimitiveCoercer.
Constructor Details
#initialize(type, strict = false) ⇒ PrimitiveCoercer
Returns a new instance of PrimitiveCoercer.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/grape/validations/types/primitive_coercer.rb', line 22 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
34 35 36 37 38 39 40 |
# File 'lib/grape/validations/types/primitive_coercer.rb', line 34 def call(val) return InvalidValue.new if reject?(val) return nil if val.nil? return '' if val == '' super end |