Class: Dry::Data::Type::Hash
Instance Attribute Summary
#constructor, #primitive
Class Method Summary
collapse
Instance Method Summary
collapse
[], #call, #constrained, #enum, #initialize, #name, #optional, passthrough_constructor, #valid?, #|
Class Method Details
.safe_constructor(hash_constructor, value_constructors, input) ⇒ Object
5
6
7
8
9
10
11
12
13
|
# File 'lib/dry/data/type/hash.rb', line 5
def self.safe_constructor(hash_constructor, value_constructors, input)
attributes = hash_constructor[input]
value_constructors.each_with_object({}) do |(key, value_constructor), result|
if attributes.key?(key)
result[key] = value_constructor[attributes[key]]
end
end
end
|
.strict_constructor(hash_constructor, value_constructors, input) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/dry/data/type/hash.rb', line 27
def self.strict_constructor(hash_constructor, value_constructors, input)
attributes = hash_constructor[input]
value_constructors.each_with_object({}) do |(key, value_constructor), result|
begin
value = attributes.fetch(key)
result[key] = value_constructor[value]
rescue TypeError
raise SchemaError.new(key, value)
rescue KeyError
raise SchemaKeyError.new(key)
end
end
end
|
.symbolized_constructor(hash_constructor, value_constructors, input) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/dry/data/type/hash.rb', line 15
def self.symbolized_constructor(hash_constructor, value_constructors, input)
attributes = hash_constructor[input]
value_constructors.each_with_object({}) do |(key, value_constructor), result|
key_name = key.to_s
if attributes.key?(key_name)
result[key.to_sym] = value_constructor[attributes[key_name]]
end
end
end
|
Instance Method Details
#schema(type_map, meth = :safe_constructor) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/dry/data/type/hash.rb', line 50
def schema(type_map, meth = :safe_constructor)
value_constructors = type_map.each_with_object({}) { |(name, type), result|
result[name] =
case type
when String, Class then Data[type]
else type
end
}
self.class.new(
self.class.method(meth).to_proc.curry.(constructor, value_constructors),
primitive
)
end
|
#strict(type_map) ⇒ Object
42
43
44
|
# File 'lib/dry/data/type/hash.rb', line 42
def strict(type_map)
schema(type_map, :strict_constructor)
end
|
#symbolized(type_map) ⇒ Object
46
47
48
|
# File 'lib/dry/data/type/hash.rb', line 46
def symbolized(type_map)
schema(type_map, :symbolized_constructor)
end
|