optify-from_hash 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/optify_from_hash/from_hashable.rb +9 -10
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 04cdd079cbaa31083b4e495031c5532361a7be37a79cd39a78e039ec47cc9644
|
|
4
|
+
data.tar.gz: '085245e223204d83447610bbe9f8b0866294b665b6e5c49fdc23cbf24cff2add'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 48a357c5882ca2533e4f77e0e01a3f9bd358cf35a8d7176c9a3f580a2e33ed1330343d21f6c0c1e9ba10eca62f1cca0d07a9b0196dfa2e32b3c9a5cb2e70e618
|
|
7
|
+
data.tar.gz: 7da7e93844a03c15b18ed9a211a387ce6a0fbd36a3e8ac1ad25d314ad3a09029f879a4def44616b583274641f94d258a982be91879652a355f908204bff88609
|
|
@@ -101,6 +101,8 @@ module Optify
|
|
|
101
101
|
unwrapped_type = _unwrap_nilable(type)
|
|
102
102
|
return value&.to_sym if unwrapped_type.is_a?(T::Types::Simple) && unwrapped_type.raw_type == Symbol
|
|
103
103
|
|
|
104
|
+
value = _convert_string(value, unwrapped_type)
|
|
105
|
+
|
|
104
106
|
case value
|
|
105
107
|
when Array
|
|
106
108
|
return _convert_array(value, unwrapped_type)
|
|
@@ -138,21 +140,18 @@ module Optify
|
|
|
138
140
|
# The hash should be a hash, but the values might be objects to convert.
|
|
139
141
|
type_for_keys = type.keys
|
|
140
142
|
type_for_values = type.values
|
|
141
|
-
type_for_keys_allows_string = _type_allows_string?(type_for_keys)
|
|
142
|
-
type_for_values_allows_string = _type_allows_string?(type_for_values)
|
|
143
143
|
|
|
144
144
|
return hash.each_with_object(Hash.new(capacity: hash.size)) do |(k, v), result|
|
|
145
|
-
result[
|
|
146
|
-
_convert_typed_hash_value(v, type_for_values, type_for_values_allows_string)
|
|
145
|
+
result[_convert_value(k, type_for_keys)] = _convert_value(v, type_for_values)
|
|
147
146
|
end
|
|
148
147
|
end
|
|
149
148
|
|
|
150
149
|
raise TypeError, "Could not convert hash #{hash} to `#{type}`."
|
|
151
150
|
end
|
|
152
151
|
|
|
153
|
-
#: (untyped, T::Types::Base
|
|
154
|
-
private_class_method def self.
|
|
155
|
-
return
|
|
152
|
+
#: (untyped, T::Types::Base) -> untyped
|
|
153
|
+
private_class_method def self._convert_string(value, type) # rubocop:disable Metrics/PerceivedComplexity
|
|
154
|
+
return value if !value.is_a?(String) || _type_allows_string?(type)
|
|
156
155
|
|
|
157
156
|
value_type = if type.respond_to?(:raw_type)
|
|
158
157
|
type #: as untyped
|
|
@@ -163,7 +162,7 @@ module Optify
|
|
|
163
162
|
if type.respond_to?(:types)
|
|
164
163
|
type #: as untyped
|
|
165
164
|
.types.each do |inner_type|
|
|
166
|
-
return
|
|
165
|
+
return _convert_string(value, inner_type)
|
|
167
166
|
rescue TypeError, ArgumentError
|
|
168
167
|
# Ignore and try the next type.
|
|
169
168
|
end
|
|
@@ -171,7 +170,7 @@ module Optify
|
|
|
171
170
|
|
|
172
171
|
return value_type.deserialize(value) if value_type && value_type != String && value_type.respond_to?(:deserialize)
|
|
173
172
|
|
|
174
|
-
|
|
173
|
+
value
|
|
175
174
|
end
|
|
176
175
|
|
|
177
176
|
#: (T::Types::Base) -> bool
|
|
@@ -197,7 +196,7 @@ module Optify
|
|
|
197
196
|
private_class_method def self._unwrap_nilable(type)
|
|
198
197
|
if type.respond_to?(:unwrap_nilable)
|
|
199
198
|
type #: as untyped
|
|
200
|
-
.unwrap_nilable
|
|
199
|
+
.unwrap_nilable || type
|
|
201
200
|
else
|
|
202
201
|
type
|
|
203
202
|
end
|