Class: Dry::Types::Definition::Hash
Instance Attribute Summary
#options, #primitive
Class Method Summary
collapse
Instance Method Summary
collapse
[], #call, #initialize, #name, #try, #valid?, #with
Methods included from Builder
#constrained, #constructor, #default, #enum, #optional, #safe, #|
Class Method Details
.safe_constructor(types, hash) ⇒ Object
5
6
7
8
9
10
11
12
13
|
# File 'lib/dry/types/definition/hash.rb', line 5
def self.safe_constructor(types, hash)
types.each_with_object({}) do |(key, type), result|
if hash.key?(key)
result[key] = type[hash[key]]
elsif type.is_a?(Default)
result[key] = type.evaluate
end
end
end
|
.strict_constructor(types, hash) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/dry/types/definition/hash.rb', line 31
def self.strict_constructor(types, hash)
types.each_with_object({}) do |(key, type), result|
begin
value = hash.fetch(key)
result[key] = type[value]
rescue TypeError
raise SchemaError.new(key, value)
rescue KeyError
raise SchemaKeyError.new(key)
end
end
end
|
.symbolized_constructor(types, hash) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/dry/types/definition/hash.rb', line 15
def self.symbolized_constructor(types, hash)
types.each_with_object({}) do |(key, type), result|
if hash.key?(key)
result[key] = type[hash[key]]
else
key_name = key.to_s
if hash.key?(key_name)
result[key] = type[hash[key_name]]
elsif type.is_a?(Default)
result[key] = type.evaluate
end
end
end
end
|
Instance Method Details
#schema(type_map, meth = :safe_constructor) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/dry/types/definition/hash.rb', line 52
def schema(type_map, meth = :safe_constructor)
types = type_map.each_with_object({}) { |(name, type), result|
result[name] =
case type
when String, Class then Types[type]
else type
end
}
fn = self.class.method(meth).to_proc.curry.(types)
constructor(fn, schema: types)
end
|
#strict(type_map) ⇒ Object
44
45
46
|
# File 'lib/dry/types/definition/hash.rb', line 44
def strict(type_map)
schema(type_map, :strict_constructor)
end
|
#symbolized(type_map) ⇒ Object
48
49
50
|
# File 'lib/dry/types/definition/hash.rb', line 48
def symbolized(type_map)
schema(type_map, :symbolized_constructor)
end
|