/
dry_type_coercer.rb
62 lines (53 loc) · 1.97 KB
/
dry_type_coercer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
module DryTypes
# Call +Dry.Types()+ to add all registered types to +DryTypes+ which is
# a container in this case. Check documentation for more information
# https://dry-rb.org/gems/dry-types/1.2/getting-started/
include Dry.Types()
end
module Grape
module Validations
module Types
# 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
# https://dry-rb.org/gems/dry-types/1.2/built-in-types/
class DryTypeCoercer
class << self
# Returns a collection coercer which corresponds to a given type.
# Example:
#
# collection_coercer_for(Array)
# #=> Grape::Validations::Types::ArrayCoercer
def collection_coercer_for(type)
Grape::Validations::Types.const_get(:"#{type.name.camelize}Coercer")
end
# Returns an instance of a coercer for a given type
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
end
def initialize(type, strict = false)
@type = type
@strict = strict
@scope = strict ? DryTypes::Strict : DryTypes::Params
end
# Coerces the given value to a type which was specified during
# initialization as a type argument.
#
# @param val [Object]
def call(val)
return if val.nil?
@coercer[val]
rescue Dry::Types::CoercionError => _e
InvalidValue.new
end
protected
attr_reader :scope, :type, :strict
end
end
end
end