k_util 0.0.17 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b57165e4a0192dcffbfd3655e45b326bcc00db6ce32d1b72116516d32d8a5c07
4
- data.tar.gz: 79ee2191737f976495009f303fb7db18f0ae9e52b6b344eb642dab5ae2884b6c
3
+ metadata.gz: 8b52770ba6a289d791e99eb4fcf868f87281200424ff7817c07e10b8dd1a4701
4
+ data.tar.gz: 23ec84b2db507096fd6593360c08570fcb3eac84676edd89f94666b81d8c1a28
5
5
  SHA512:
6
- metadata.gz: 21de731f6f1d53e8d1a28df7e1f5d80058955a517d774960f0d7362c778062cac0e9445c5d6d2cde6a88a9ce1619810dce9d5103b8a5b62d83707fce34aaabc3
7
- data.tar.gz: 6d062276df66896e21ffa92e07dd25d38f7951da46fcaba6ab5ced669cedd7decd669df11a7902b251589e2f17dda4bb2d7aaf61911f5c08277e20a208b6a314
6
+ metadata.gz: 78ee597e330cda00891023bb95bbbacfe07178accad429d4724805a7f60f853d0ace04c74ff91c540b3a71a34ef722e3cd1606092c957ac5a368aa7c440a38cd
7
+ data.tar.gz: 0f7f5913e389eb066f42819040211d68791735a32b9c1af3572fdd08712adcbe4ddfb5c1e3732004be89ef4ba842ef5a339887021426e1e655b6d06ed64135f6
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
@@ -6,62 +6,48 @@ module KUtil
6
6
  class DataHelper
7
7
  # Convert JSON string into to_open_struct but designed to work with a JSON string
8
8
  #
9
+ # KUtil.data.parse_json(json, as: :symbolize_keys)
9
10
  # https://docs.ruby-lang.org/en/master/JSON.html
10
11
  # rubocop:disable Naming/MethodParameterName
11
- def json_parse(json, as: :hash)
12
+ def parse_json(json, as: :hash)
13
+ log.block(%i[hash hash_symbolized open_struct], title: 'Help as: ?') if as == :help
14
+
12
15
  case as
13
16
  when :hash
14
17
  JSON.parse(json)
15
- when :hash_symbolized
18
+ when :hash_symbolized, :symbolize_names, :symbolize_keys
16
19
  JSON.parse(json, symbolize_names: true)
17
20
  when :open_struct
18
21
  JSON.parse(json, object_class: OpenStruct)
19
22
  end
20
23
  end
24
+ alias json_parse parse_json
21
25
  # rubocop:enable Naming/MethodParameterName
22
26
 
23
27
  # Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct
24
28
  #
25
29
  # or an array of deep nested OpenStruct
26
30
  def to_open_struct(data)
27
- case data
28
- when Hash
29
- OpenStruct.new(data.transform_values { |v| to_open_struct(v) })
30
-
31
- when Array
32
- data.map { |o| to_open_struct(o) }
31
+ return OpenStruct.new(data.transform_values { |v| to_open_struct(v) }) if data.is_a?(Hash)
32
+ return data.map { |o| to_open_struct(o) } if data.is_a?(Array)
33
+ return to_open_struct(data.to_h) if hash_convertible?(data)
33
34
 
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
35
+ # Some primitave type: String, True/False or an ObjectStruct
36
+ data
41
37
  end
42
38
 
43
39
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
40
+ # Convert data to hash and deal with mixed data types such as Struct and OpenStruct
44
41
  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
42
+ # This nil check is only for the root object
43
+ return {} if data.nil?
44
+
45
+ return data.map { |value| hash_convertible?(value) ? to_hash(value) : value } if data.is_a?(Array)
49
46
 
50
47
  return to_hash(data.to_h) if !data.is_a?(Hash) && data.respond_to?(:to_h)
51
48
 
52
49
  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
50
+ hash[key] = hash_convertible?(value) ? to_hash(value) : value
65
51
  end
66
52
  end
67
53
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -72,19 +58,41 @@ module KUtil
72
58
  value.is_a?(Symbol) ? value.to_s : value
73
59
  end
74
60
 
75
- # Add if needed
76
- # # Is the value a basic (aka primitive) type
77
- # def basic_type?(value)
78
- # value.is_a?(String) ||
79
- # value.is_a?(Symbol) ||
80
- # value.is_a?(FalseClass) ||
81
- # value.is_a?(TrueClass) ||
82
- # value.is_a?(Integer) ||
83
- # value.is_a?(Float)
61
+ def deep_symbolize_keys(input)
62
+ return input if input.nil?
63
+
64
+ return input unless input.is_a?(Hash)
65
+
66
+ input.each_with_object({}) do |key_value, new_hash|
67
+ key, value = key_value
68
+ value = deep_symbolize_keys(value) if value.is_a?(Hash)
69
+ value = value.map { |v| deep_symbolize_keys(v) } if value.is_a?(Array)
70
+ new_hash[key.to_sym] = value
71
+ end
72
+ end
73
+
74
+ # def deep_symbolize_keys(hash)
75
+ # return hash if hash.nil?
76
+
77
+ # hash.transform_keys(&:to_sym)
84
78
  # end
85
79
 
80
+ # Is the value a basic (aka primitive) type
81
+ def basic_type?(value)
82
+ value.is_a?(String) ||
83
+ value.is_a?(Symbol) ||
84
+ value.is_a?(FalseClass) ||
85
+ value.is_a?(TrueClass) ||
86
+ value.is_a?(Integer) ||
87
+ value.is_a?(Float)
88
+ end
89
+
86
90
  # Is the value a complex container type, but not a regular class.
87
91
  def hash_convertible?(value)
92
+ # Nil is a special case, it responds to :to_h but generally
93
+ # you only want to convert nil to {} in specific scenarios
94
+ return false if value.nil?
95
+
88
96
  value.is_a?(Array) ||
89
97
  value.is_a?(Hash) ||
90
98
  value.is_a?(Struct) ||
@@ -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.24'
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.24
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-08-28 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"