class Grape::Validations::Types::DryTypeCoercer
A base class for classes which must identify a coercer to be used. If the strict
argument is true, it won’t coerce the given value but check its type. More information there dry-rb.org/gems/dry-types/1.2/built-in-types/
Attributes
Public Class Methods
Source
# File lib/grape/validations/types/dry_type_coercer.rb, line 36 def coercer_instance_for(type, strict = false) klass = type.instance_of?(Class) ? PrimitiveCoercer : collection_coercer_for(type) klass.new(type, strict) end
Returns an instance of a coercer for a given type
Source
# File lib/grape/validations/types/dry_type_coercer.rb, line 24 def collection_coercer_for(type) case type when Array ArrayCoercer when Set SetCoercer else raise ArgumentError, "Unknown type: #{type}" end end
Returns a collection coercer which corresponds to a given type. Example:
collection_coercer_for(Array) #=> Grape::Validations::Types::ArrayCoercer
Source
# File lib/grape/validations/types/dry_type_coercer.rb, line 42 def initialize(type, strict = false) @type = type @strict = strict @scope = strict ? DryTypes::Strict : DryTypes::Params end
Public Instance Methods
Source
# File lib/grape/validations/types/dry_type_coercer.rb, line 52 def call(val) return if val.nil? @coercer[val] rescue Dry::Types::CoercionError => _e InvalidValue.new end
Coerces the given value to a type which was specified during initialization as a type argument.
@param val [Object]