optify-from_hash 0.2.0 → 0.2.1
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 +19 -15
- 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: 0e24ce9b23b87819fc6688195ca5a90cb43dfbe23b25264e269cf37daa97f531
|
|
4
|
+
data.tar.gz: 18e142eb6c75bf80cfab5fb8d64079ec1f7fd2d212e97043def5555d595fcdd0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ecfa102d64db21405131a02545bfa524eebde5150c9030555faa89e8e802ec6bdf8006fbd90d30a713450a1beb28167302996bbee2507c01f6d67cea3196ad56
|
|
7
|
+
data.tar.gz: 1943ab3ab4ec77e5441de4006b1699683a5bf0d9e8151d3b51ef4fcedd00e6eedf5b8baa5e9d42e7edff367f22d29109495e63ee2e6cff197facd2f5edd8ed79
|
|
@@ -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,7 +39,7 @@ 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.
|
|
@@ -52,7 +51,10 @@ module Optify
|
|
|
52
51
|
case value
|
|
53
52
|
when Array
|
|
54
53
|
# Handle `T.nilable(T::Array[...])`
|
|
55
|
-
|
|
54
|
+
if type.respond_to?(:unwrap_nilable)
|
|
55
|
+
type = type #: as untyped
|
|
56
|
+
.unwrap_nilable
|
|
57
|
+
end
|
|
56
58
|
inner_type = type.type
|
|
57
59
|
return value.map { |v| _convert_value(v, inner_type) }.freeze
|
|
58
60
|
when Hash
|
|
@@ -61,7 +63,8 @@ module Optify
|
|
|
61
63
|
# `T.any(...)` because using `.types` works for both cases.
|
|
62
64
|
if type.respond_to?(:types)
|
|
63
65
|
# Find a type that works for the hash.
|
|
64
|
-
type
|
|
66
|
+
type #: as untyped
|
|
67
|
+
.types.each do |t|
|
|
65
68
|
return _convert_hash(value, t).freeze
|
|
66
69
|
rescue StandardError
|
|
67
70
|
# Ignore and try the next type.
|
|
@@ -76,12 +79,13 @@ module Optify
|
|
|
76
79
|
value
|
|
77
80
|
end
|
|
78
81
|
|
|
79
|
-
#: (Hash[untyped, untyped],
|
|
82
|
+
#: (Hash[untyped, untyped], T::Types::Base) -> untyped
|
|
80
83
|
def self._convert_hash(hash, type)
|
|
81
84
|
if type.respond_to?(:raw_type)
|
|
82
85
|
# There is an object for the hash.
|
|
83
86
|
# It could be a custom class, a String, or maybe something else.
|
|
84
|
-
type_for_hash = type
|
|
87
|
+
type_for_hash = type #: as untyped
|
|
88
|
+
.raw_type
|
|
85
89
|
return type_for_hash.from_hash(hash) if type_for_hash.respond_to?(:from_hash)
|
|
86
90
|
elsif type.is_a?(T::Types::TypedHash)
|
|
87
91
|
# The hash should be a hash, but the values might be objects to convert.
|
|
@@ -116,9 +120,9 @@ module Optify
|
|
|
116
120
|
end
|
|
117
121
|
|
|
118
122
|
# Convert this object to a JSON string.
|
|
119
|
-
#: (
|
|
120
|
-
def to_json(
|
|
121
|
-
to_h.to_json(
|
|
123
|
+
#: (?JSON::State?) -> String
|
|
124
|
+
def to_json(state = nil)
|
|
125
|
+
to_h.to_json(state)
|
|
122
126
|
end
|
|
123
127
|
|
|
124
128
|
# Convert this object to a Hash recursively.
|
|
@@ -134,21 +138,19 @@ module Optify
|
|
|
134
138
|
# Remove the @ prefix to get the method name
|
|
135
139
|
method_name = var_name.to_s[1..] #: as !nil
|
|
136
140
|
value = instance_variable_get(var_name)
|
|
137
|
-
result[method_name.to_sym] =
|
|
141
|
+
result[method_name.to_sym] = self.class.send(:_convert_value_for_to_h, value)
|
|
138
142
|
end
|
|
139
143
|
|
|
140
144
|
result
|
|
141
145
|
end
|
|
142
146
|
|
|
143
|
-
private
|
|
144
|
-
|
|
145
147
|
#: (untyped) -> untyped
|
|
146
|
-
def
|
|
148
|
+
def self._convert_value_for_to_h(value)
|
|
147
149
|
case value
|
|
148
150
|
when Array
|
|
149
|
-
value.map { |v|
|
|
151
|
+
value.map { |v| _convert_value_for_to_h(v) }
|
|
150
152
|
when Hash
|
|
151
|
-
value.transform_values { |v|
|
|
153
|
+
value.transform_values { |v| _convert_value_for_to_h(v) }
|
|
152
154
|
when nil
|
|
153
155
|
nil
|
|
154
156
|
else
|
|
@@ -159,5 +161,7 @@ module Optify
|
|
|
159
161
|
end
|
|
160
162
|
end
|
|
161
163
|
end
|
|
164
|
+
|
|
165
|
+
private_class_method :_convert_value_for_to_h
|
|
162
166
|
end
|
|
163
167
|
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`,
|