k_util 0.0.17 → 0.0.19

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: b57165e4a0192dcffbfd3655e45b326bcc00db6ce32d1b72116516d32d8a5c07
4
- data.tar.gz: 79ee2191737f976495009f303fb7db18f0ae9e52b6b344eb642dab5ae2884b6c
3
+ metadata.gz: f49e3c0ac1db1cfaf2c310205d474b0e2c24c322fad152a172bed137f7f31d2f
4
+ data.tar.gz: 2029369204a126de193b01e0219e4b37dc3e0e1745b45cdde01cdc2a159a58cf
5
5
  SHA512:
6
- metadata.gz: 21de731f6f1d53e8d1a28df7e1f5d80058955a517d774960f0d7362c778062cac0e9445c5d6d2cde6a88a9ce1619810dce9d5103b8a5b62d83707fce34aaabc3
7
- data.tar.gz: 6d062276df66896e21ffa92e07dd25d38f7951da46fcaba6ab5ced669cedd7decd669df11a7902b251589e2f17dda4bb2d7aaf61911f5c08277e20a208b6a314
6
+ metadata.gz: 3f0d2550416e6bd606a107b353cc7e2226f59cf22375fac03afac4f59306a2a40622af2a27d0ea0f5fe31577318cd6c39a671b26bd8503d5c8f1c31fe5622fe2
7
+ data.tar.gz: 63a7cef3672ed4b80a38547272cce5564b68ae795b38d1972ba4d9f2d82ccb16006c96ee58f1ccdf5626f56608e3c45a78609953add05ae32747ba11d7ae3fde
data/Gemfile CHANGED
@@ -23,3 +23,7 @@ group :development, :test do
23
23
  gem 'rubocop-rake', require: false
24
24
  gem 'rubocop-rspec', require: false
25
25
  end
26
+
27
+ group :test do
28
+ gem 'dry-struct', '~> 1'
29
+ end
@@ -24,44 +24,25 @@ module KUtil
24
24
  #
25
25
  # or an array of deep nested OpenStruct
26
26
  def to_open_struct(data)
27
- case data
28
- when Hash
29
- OpenStruct.new(data.transform_values { |v| to_open_struct(v) })
27
+ return OpenStruct.new(data.transform_values { |v| to_open_struct(v) }) if data.is_a?(Hash)
28
+ return data.map { |o| to_open_struct(o) } if data.is_a?(Array)
29
+ return to_open_struct(data.to_h) if hash_convertible?(data)
30
30
 
31
- when Array
32
- data.map { |o| to_open_struct(o) }
33
-
34
- when Struct, OpenStruct
35
- to_open_struct(data.to_h)
36
-
37
- else
38
- # Some primitave type: String, True/False or an ObjectStruct
39
- data
40
- end
31
+ # Some primitave type: String, True/False or an ObjectStruct
32
+ data
41
33
  end
42
34
 
43
35
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
36
+ # Convert data to hash and deal with mixed data types such as Struct and OpenStruct
44
37
  def to_hash(data)
45
- # No test yet
46
- if data.is_a?(Array)
47
- return data.map { |v| v.is_a?(OpenStruct) ? to_hash(v) : v }
48
- end
38
+ return {} if data.nil?
39
+
40
+ return data.map { |value| hash_convertible?(value) ? to_hash(value) : value } if data.is_a?(Array)
49
41
 
50
42
  return to_hash(data.to_h) if !data.is_a?(Hash) && data.respond_to?(:to_h)
51
43
 
52
44
  data.each_pair.with_object({}) do |(key, value), hash|
53
- case value
54
- when OpenStruct, Struct, Hash
55
- hash[key] = to_hash(value)
56
- when Array
57
- # No test yet
58
- values = value.map do |v|
59
- v.is_a?(OpenStruct) || v.is_a?(Struct) || v.is_a?(Hash) ? to_hash(v) : v
60
- end
61
- hash[key] = values
62
- else
63
- hash[key] = value
64
- end
45
+ hash[key] = hash_convertible?(value) ? to_hash(value) : value
65
46
  end
66
47
  end
67
48
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KUtil
4
- VERSION = '0.0.17'
4
+ VERSION = '0.0.19'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: k_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-12 00:00:00.000000000 Z
11
+ date: 2021-07-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: " KUtil provides simple utility methods, such as file helpers, data
14
14
  object helpers and more.\n"