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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab3e37acc0afa16b17fbfb4bcbbf95e31b38d75c321d3d3d3a9a9f8a9fb7afb5
4
- data.tar.gz: b087d7433c8329a6e62262d129f80cd538dae2459337e619ad2ab760df21bbc5
3
+ metadata.gz: 0e24ce9b23b87819fc6688195ca5a90cb43dfbe23b25264e269cf37daa97f531
4
+ data.tar.gz: 18e142eb6c75bf80cfab5fb8d64079ec1f7fd2d212e97043def5555d595fcdd0
5
5
  SHA512:
6
- metadata.gz: 968c5aa5b223c043a8558f5e2ac2b4cf8efb7925ed408bf6dffc458734a51a6da0d4b85e781688354b8432c0b6307067c4102521f7792d801859a1a80bdf391c
7
- data.tar.gz: 57b11b0707b45887e544ad8d32286e0780a37d89f1fc87ea293139ed13a26140e2cde81cf1afe534827e4c9be69e6a3c5812b236b2ed5fd6438e73e1985ed206
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, untyped) -> 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
- type = type.unwrap_nilable if type.respond_to?(:unwrap_nilable)
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.types.each do |t|
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], 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.raw_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
- #: (*untyped) -> String
120
- def to_json(*args)
121
- to_h.to_json(args)
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] = _convert_value_to_hash(value)
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 _convert_value_to_hash(value)
148
+ def self._convert_value_for_to_h(value)
147
149
  case value
148
150
  when Array
149
- value.map { |v| _convert_value_to_hash(v) }
151
+ value.map { |v| _convert_value_for_to_h(v) }
150
152
  when Hash
151
- value.transform_values { |v| _convert_value_to_hash(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
@@ -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(args: T.untyped).returns(String) }
20
- def to_json(*args); end
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`,
@@ -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: (*untyped args) -> String
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`,
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin D. Harris