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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1632ba5c0ae9ac2b6b30c9a60ba78b00cafb89ec15199fa7ba994e2d1fc90539
4
- data.tar.gz: 9cd72e917b0688f61f5a0ec2d0d3ecb85f4939cdcd21b9a7bbdd494af67e9a93
3
+ metadata.gz: 04cdd079cbaa31083b4e495031c5532361a7be37a79cd39a78e039ec47cc9644
4
+ data.tar.gz: '085245e223204d83447610bbe9f8b0866294b665b6e5c49fdc23cbf24cff2add'
5
5
  SHA512:
6
- metadata.gz: 9d17e0f0d1bd9e8f868b168fb90ce0edaf3de4445b11780c48e24f790ed10b98c455b1c489d085658313043a6e6e7c8773d9aa744bf5b64d4f9a7423547ad3ba
7
- data.tar.gz: c903dc3d0ca0fe29ff6a8c3dd50e68e9c1d946d66b8fb96db1cdd45df9104f8994b5c661485e9430bf5f73819183e08a95873f3730e658feb6f9c1ee67b9502c
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[_convert_typed_hash_value(k, type_for_keys, type_for_keys_allows_string)] =
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, bool) -> untyped
154
- private_class_method def self._convert_typed_hash_value(value, type, type_allows_string) # rubocop:disable Metrics/PerceivedComplexity
155
- return _convert_value(value, type) if type_allows_string || !value.is_a?(String)
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 _convert_typed_hash_value(value, inner_type, _type_allows_string?(inner_type))
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
- _convert_value(value, type)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optify-from_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin D. Harris