optify-from_hash 0.2.0 → 0.2.2
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 +31 -19
- data/rbi/optify_from_hash.rbi +2 -3
- data/sig/optify_from_hash.rbs +1 -2
- 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: 1f6e409cfccf83b515e84d3cc19713e1897ed53cf4340d66de2843433c355445
|
|
4
|
+
data.tar.gz: 630ed4471403551570ba2f18d3ec687ba4790dec90332496d6f9d39100f353f5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b9414cd9843a25f5b1ed1ab7f1560d5e73c0f85b3900859daa786c03f02bfd04dbc4947120bf519c8ff5f80b3712947b3b53a20c778b1e8e354c259b7c266e19
|
|
7
|
+
data.tar.gz: de4275bb6b2300dfa3e154dbc04ec49120d28ec3f20e17e537c51e1cde5a065772eaad10269abdeb8fa7a528043b376a49d2c2655d810b47373137f322a39fdc
|
|
@@ -14,7 +14,6 @@ module Optify
|
|
|
14
14
|
|
|
15
15
|
# Create a new immutable instance of the class from a hash.
|
|
16
16
|
#
|
|
17
|
-
# This is a class method so that it can set members with private setters.
|
|
18
17
|
# @param hash The hash to create the instance from.
|
|
19
18
|
# @return The new instance.
|
|
20
19
|
#: (Hash[untyped, untyped]) -> instance
|
|
@@ -40,20 +39,20 @@ module Optify
|
|
|
40
39
|
instance.freeze
|
|
41
40
|
end
|
|
42
41
|
|
|
43
|
-
#: (untyped,
|
|
42
|
+
#: (untyped, T::Types::Base) -> untyped
|
|
44
43
|
def self._convert_value(value, type)
|
|
45
44
|
if type.is_a?(T::Types::Untyped)
|
|
46
45
|
# No preferred type is given, so return the value as is.
|
|
47
46
|
return value
|
|
48
47
|
end
|
|
49
48
|
|
|
50
|
-
|
|
49
|
+
unwrapped_type = _unwrap_nilable(type)
|
|
50
|
+
return value&.to_sym if unwrapped_type.is_a?(T::Types::Simple) && unwrapped_type.raw_type == Symbol
|
|
51
51
|
|
|
52
52
|
case value
|
|
53
53
|
when Array
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
inner_type = type.type
|
|
54
|
+
inner_type = unwrapped_type #: as untyped
|
|
55
|
+
.type
|
|
57
56
|
return value.map { |v| _convert_value(v, inner_type) }.freeze
|
|
58
57
|
when Hash
|
|
59
58
|
# Handle `T.nilable(T::Hash[...])` and `T.any(...)`.
|
|
@@ -61,7 +60,8 @@ module Optify
|
|
|
61
60
|
# `T.any(...)` because using `.types` works for both cases.
|
|
62
61
|
if type.respond_to?(:types)
|
|
63
62
|
# Find a type that works for the hash.
|
|
64
|
-
type
|
|
63
|
+
type #: as untyped
|
|
64
|
+
.types.each do |t|
|
|
65
65
|
return _convert_hash(value, t).freeze
|
|
66
66
|
rescue StandardError
|
|
67
67
|
# Ignore and try the next type.
|
|
@@ -76,12 +76,13 @@ module Optify
|
|
|
76
76
|
value
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
-
#: (Hash[untyped, untyped],
|
|
79
|
+
#: (Hash[untyped, untyped], T::Types::Base) -> untyped
|
|
80
80
|
def self._convert_hash(hash, type)
|
|
81
81
|
if type.respond_to?(:raw_type)
|
|
82
82
|
# There is an object for the hash.
|
|
83
83
|
# It could be a custom class, a String, or maybe something else.
|
|
84
|
-
type_for_hash = type
|
|
84
|
+
type_for_hash = type #: as untyped
|
|
85
|
+
.raw_type
|
|
85
86
|
return type_for_hash.from_hash(hash) if type_for_hash.respond_to?(:from_hash)
|
|
86
87
|
elsif type.is_a?(T::Types::TypedHash)
|
|
87
88
|
# The hash should be a hash, but the values might be objects to convert.
|
|
@@ -100,7 +101,18 @@ module Optify
|
|
|
100
101
|
raise TypeError, "Could not convert hash #{hash} to `#{type}`."
|
|
101
102
|
end
|
|
102
103
|
|
|
103
|
-
|
|
104
|
+
# Unwrap `T.nilable(...)` to get the inner type, or return the type as-is.
|
|
105
|
+
#: (T::Types::Base) -> T::Types::Base
|
|
106
|
+
def self._unwrap_nilable(type)
|
|
107
|
+
if type.respond_to?(:unwrap_nilable)
|
|
108
|
+
type #: as untyped
|
|
109
|
+
.unwrap_nilable
|
|
110
|
+
else
|
|
111
|
+
type
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private_class_method :_convert_hash, :_convert_value, :_unwrap_nilable
|
|
104
116
|
|
|
105
117
|
# Compare this object with another object for equality.
|
|
106
118
|
# @param other The object to compare.
|
|
@@ -116,9 +128,9 @@ module Optify
|
|
|
116
128
|
end
|
|
117
129
|
|
|
118
130
|
# Convert this object to a JSON string.
|
|
119
|
-
#: (
|
|
120
|
-
def to_json(
|
|
121
|
-
to_h.to_json(
|
|
131
|
+
#: (?JSON::State?) -> String
|
|
132
|
+
def to_json(state = nil)
|
|
133
|
+
to_h.to_json(state)
|
|
122
134
|
end
|
|
123
135
|
|
|
124
136
|
# Convert this object to a Hash recursively.
|
|
@@ -134,21 +146,19 @@ module Optify
|
|
|
134
146
|
# Remove the @ prefix to get the method name
|
|
135
147
|
method_name = var_name.to_s[1..] #: as !nil
|
|
136
148
|
value = instance_variable_get(var_name)
|
|
137
|
-
result[method_name.to_sym] =
|
|
149
|
+
result[method_name.to_sym] = self.class.send(:_convert_value_for_to_h, value)
|
|
138
150
|
end
|
|
139
151
|
|
|
140
152
|
result
|
|
141
153
|
end
|
|
142
154
|
|
|
143
|
-
private
|
|
144
|
-
|
|
145
155
|
#: (untyped) -> untyped
|
|
146
|
-
def
|
|
156
|
+
def self._convert_value_for_to_h(value)
|
|
147
157
|
case value
|
|
148
158
|
when Array
|
|
149
|
-
value.map { |v|
|
|
159
|
+
value.map { |v| _convert_value_for_to_h(v) }
|
|
150
160
|
when Hash
|
|
151
|
-
value.transform_values { |v|
|
|
161
|
+
value.transform_values { |v| _convert_value_for_to_h(v) }
|
|
152
162
|
when nil
|
|
153
163
|
nil
|
|
154
164
|
else
|
|
@@ -159,5 +169,7 @@ module Optify
|
|
|
159
169
|
end
|
|
160
170
|
end
|
|
161
171
|
end
|
|
172
|
+
|
|
173
|
+
private_class_method :_convert_value_for_to_h
|
|
162
174
|
end
|
|
163
175
|
end
|
data/rbi/optify_from_hash.rbi
CHANGED
|
@@ -9,15 +9,14 @@ module Optify
|
|
|
9
9
|
|
|
10
10
|
# Create a new instance of the class from a hash.
|
|
11
11
|
#
|
|
12
|
-
# This is a class method that so that it can set members with private setters.
|
|
13
12
|
# @param hash The hash to create the instance from.
|
|
14
13
|
# @return The new instance.
|
|
15
14
|
sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T.attached_class) }
|
|
16
15
|
def self.from_hash(hash); end
|
|
17
16
|
|
|
18
17
|
# Convert this object to a JSON string.
|
|
19
|
-
sig { params(
|
|
20
|
-
def to_json(
|
|
18
|
+
sig { params(state: T.nilable(JSON::State)).returns(String) }
|
|
19
|
+
def to_json(state = nil); end
|
|
21
20
|
|
|
22
21
|
# Convert this object to a Hash recursively.
|
|
23
22
|
# This is mostly the reverse operation of `from_hash`,
|
data/sig/optify_from_hash.rbs
CHANGED
|
@@ -6,13 +6,12 @@ end
|
|
|
6
6
|
class Optify::FromHashable
|
|
7
7
|
# Create a new instance of the class from a hash.
|
|
8
8
|
#
|
|
9
|
-
# This is a class method that so that it can set members with private setters.
|
|
10
9
|
# @param hash The hash to create the instance from.
|
|
11
10
|
# @return The new instance.
|
|
12
11
|
def self.from_hash: (::Hash[untyped, untyped] hash) -> instance
|
|
13
12
|
|
|
14
13
|
# Convert this object to a JSON string.
|
|
15
|
-
def to_json: (
|
|
14
|
+
def to_json: (?JSON::State? state) -> String
|
|
16
15
|
|
|
17
16
|
# Convert this object to a Hash recursively.
|
|
18
17
|
# This is mostly the reverse operation of `from_hash`,
|