optify-from_hash 0.1.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 +23 -12
- data/rbi/optify_from_hash.rbi +4 -3
- data/sig/optify_from_hash.rbs +3 -3
- metadata +2 -2
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
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# typed: strict
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
+
require 'json'
|
|
4
5
|
require 'sorbet-runtime'
|
|
5
6
|
require 'tapioca'
|
|
6
7
|
|
|
@@ -13,7 +14,6 @@ module Optify
|
|
|
13
14
|
|
|
14
15
|
# Create a new immutable instance of the class from a hash.
|
|
15
16
|
#
|
|
16
|
-
# This is a class method so that it can set members with private setters.
|
|
17
17
|
# @param hash The hash to create the instance from.
|
|
18
18
|
# @return The new instance.
|
|
19
19
|
#: (Hash[untyped, untyped]) -> instance
|
|
@@ -39,7 +39,7 @@ module Optify
|
|
|
39
39
|
instance.freeze
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
-
#: (untyped,
|
|
42
|
+
#: (untyped, T::Types::Base) -> untyped
|
|
43
43
|
def self._convert_value(value, type)
|
|
44
44
|
if type.is_a?(T::Types::Untyped)
|
|
45
45
|
# No preferred type is given, so return the value as is.
|
|
@@ -51,7 +51,10 @@ module Optify
|
|
|
51
51
|
case value
|
|
52
52
|
when Array
|
|
53
53
|
# Handle `T.nilable(T::Array[...])`
|
|
54
|
-
|
|
54
|
+
if type.respond_to?(:unwrap_nilable)
|
|
55
|
+
type = type #: as untyped
|
|
56
|
+
.unwrap_nilable
|
|
57
|
+
end
|
|
55
58
|
inner_type = type.type
|
|
56
59
|
return value.map { |v| _convert_value(v, inner_type) }.freeze
|
|
57
60
|
when Hash
|
|
@@ -60,7 +63,8 @@ module Optify
|
|
|
60
63
|
# `T.any(...)` because using `.types` works for both cases.
|
|
61
64
|
if type.respond_to?(:types)
|
|
62
65
|
# Find a type that works for the hash.
|
|
63
|
-
type
|
|
66
|
+
type #: as untyped
|
|
67
|
+
.types.each do |t|
|
|
64
68
|
return _convert_hash(value, t).freeze
|
|
65
69
|
rescue StandardError
|
|
66
70
|
# Ignore and try the next type.
|
|
@@ -75,12 +79,13 @@ module Optify
|
|
|
75
79
|
value
|
|
76
80
|
end
|
|
77
81
|
|
|
78
|
-
#: (Hash[untyped, untyped],
|
|
82
|
+
#: (Hash[untyped, untyped], T::Types::Base) -> untyped
|
|
79
83
|
def self._convert_hash(hash, type)
|
|
80
84
|
if type.respond_to?(:raw_type)
|
|
81
85
|
# There is an object for the hash.
|
|
82
86
|
# It could be a custom class, a String, or maybe something else.
|
|
83
|
-
type_for_hash = type
|
|
87
|
+
type_for_hash = type #: as untyped
|
|
88
|
+
.raw_type
|
|
84
89
|
return type_for_hash.from_hash(hash) if type_for_hash.respond_to?(:from_hash)
|
|
85
90
|
elsif type.is_a?(T::Types::TypedHash)
|
|
86
91
|
# The hash should be a hash, but the values might be objects to convert.
|
|
@@ -114,6 +119,12 @@ module Optify
|
|
|
114
119
|
end
|
|
115
120
|
end
|
|
116
121
|
|
|
122
|
+
# Convert this object to a JSON string.
|
|
123
|
+
#: (?JSON::State?) -> String
|
|
124
|
+
def to_json(state = nil)
|
|
125
|
+
to_h.to_json(state)
|
|
126
|
+
end
|
|
127
|
+
|
|
117
128
|
# Convert this object to a Hash recursively.
|
|
118
129
|
# This is mostly the reverse operation of `from_hash`,
|
|
119
130
|
# as keys will be symbols
|
|
@@ -127,21 +138,19 @@ module Optify
|
|
|
127
138
|
# Remove the @ prefix to get the method name
|
|
128
139
|
method_name = var_name.to_s[1..] #: as !nil
|
|
129
140
|
value = instance_variable_get(var_name)
|
|
130
|
-
result[method_name.to_sym] =
|
|
141
|
+
result[method_name.to_sym] = self.class.send(:_convert_value_for_to_h, value)
|
|
131
142
|
end
|
|
132
143
|
|
|
133
144
|
result
|
|
134
145
|
end
|
|
135
146
|
|
|
136
|
-
private
|
|
137
|
-
|
|
138
147
|
#: (untyped) -> untyped
|
|
139
|
-
def
|
|
148
|
+
def self._convert_value_for_to_h(value)
|
|
140
149
|
case value
|
|
141
150
|
when Array
|
|
142
|
-
value.map { |v|
|
|
151
|
+
value.map { |v| _convert_value_for_to_h(v) }
|
|
143
152
|
when Hash
|
|
144
|
-
value.transform_values { |v|
|
|
153
|
+
value.transform_values { |v| _convert_value_for_to_h(v) }
|
|
145
154
|
when nil
|
|
146
155
|
nil
|
|
147
156
|
else
|
|
@@ -152,5 +161,7 @@ module Optify
|
|
|
152
161
|
end
|
|
153
162
|
end
|
|
154
163
|
end
|
|
164
|
+
|
|
165
|
+
private_class_method :_convert_value_for_to_h
|
|
155
166
|
end
|
|
156
167
|
end
|
data/rbi/optify_from_hash.rbi
CHANGED
|
@@ -5,18 +5,19 @@
|
|
|
5
5
|
module Optify
|
|
6
6
|
# A base class for classes that can be created from a hash.
|
|
7
7
|
class FromHashable
|
|
8
|
-
extend T::Sig
|
|
9
|
-
extend T::Helpers
|
|
10
8
|
abstract!
|
|
11
9
|
|
|
12
10
|
# Create a new instance of the class from a hash.
|
|
13
11
|
#
|
|
14
|
-
# This is a class method that so that it can set members with private setters.
|
|
15
12
|
# @param hash The hash to create the instance from.
|
|
16
13
|
# @return The new instance.
|
|
17
14
|
sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T.attached_class) }
|
|
18
15
|
def self.from_hash(hash); end
|
|
19
16
|
|
|
17
|
+
# Convert this object to a JSON string.
|
|
18
|
+
sig { params(state: T.nilable(JSON::State)).returns(String) }
|
|
19
|
+
def to_json(state = nil); end
|
|
20
|
+
|
|
20
21
|
# Convert this object to a Hash recursively.
|
|
21
22
|
# This is mostly the reverse operation of `from_hash`,
|
|
22
23
|
# as keys will be symbols
|
data/sig/optify_from_hash.rbs
CHANGED
|
@@ -4,15 +4,15 @@ end
|
|
|
4
4
|
|
|
5
5
|
# A base class for classes that can be created from a hash.
|
|
6
6
|
class Optify::FromHashable
|
|
7
|
-
extend T::Helpers
|
|
8
|
-
|
|
9
7
|
# Create a new instance of the class from a hash.
|
|
10
8
|
#
|
|
11
|
-
# This is a class method that so that it can set members with private setters.
|
|
12
9
|
# @param hash The hash to create the instance from.
|
|
13
10
|
# @return The new instance.
|
|
14
11
|
def self.from_hash: (::Hash[untyped, untyped] hash) -> instance
|
|
15
12
|
|
|
13
|
+
# Convert this object to a JSON string.
|
|
14
|
+
def to_json: (?JSON::State? state) -> String
|
|
15
|
+
|
|
16
16
|
# Convert this object to a Hash recursively.
|
|
17
17
|
# This is mostly the reverse operation of `from_hash`,
|
|
18
18
|
# as keys will be symbols
|
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.1
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Justin D. Harris
|
|
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
134
134
|
- !ruby/object:Gem::Version
|
|
135
135
|
version: '0'
|
|
136
136
|
requirements: []
|
|
137
|
-
rubygems_version: 3.7
|
|
137
|
+
rubygems_version: 3.6.7
|
|
138
138
|
specification_version: 4
|
|
139
139
|
summary: Utilities for converting hashes to immutable objects.
|
|
140
140
|
test_files: []
|