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
scope[R]
strict[R]
type[R]
Public Class Methods
coercer_instance_for(type, strict = false)
click to toggle source
Returns an instance of a coercer for a given type
# File lib/grape/validations/types/dry_type_coercer.rb, line 37 def coercer_instance_for(type, strict = false) return PrimitiveCoercer.new(type, strict) if type.instance_of?(Class) # in case of a collection (Array[Integer]) the type is an instance of a collection, # so we need to figure out the actual type collection_coercer_for(type.class).new(type, strict) end
collection_coercer_for(type)
click to toggle source
Returns a collection coercer which corresponds to a given type. Example:
collection_coercer_for(Array) #=> Grape::Validations::Types::ArrayCoercer
# File lib/grape/validations/types/dry_type_coercer.rb, line 32 def collection_coercer_for(type) collection_coercers[type] end
new(type, strict = false)
click to toggle source
# File lib/grape/validations/types/dry_type_coercer.rb, line 52 def initialize(type, strict = false) @type = type @strict = strict @scope = strict ? DryTypes::Strict : DryTypes::Params end
register_collection(type)
click to toggle source
Registers a collection coercer which could be found by a type, see collection_coercer_for
method below. This method is meant for inheritors.
# File lib/grape/validations/types/dry_type_coercer.rb, line 23 def register_collection(type) DryTypeCoercer.collection_coercers[type] = self end
Protected Class Methods
collection_coercers()
click to toggle source
# File lib/grape/validations/types/dry_type_coercer.rb, line 47 def collection_coercers @collection_coercers ||= {} end
Public Instance Methods
call(val)
click to toggle source
Coerces the given value to a type which was specified during initialization as a type argument.
@param val [Object]
# File lib/grape/validations/types/dry_type_coercer.rb, line 62 def call(val) return if val.nil? @coercer[val] rescue Dry::Types::CoercionError => _e InvalidValue.new end