k_util 0.0.15 → 0.0.23
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/Gemfile +4 -0
- data/lib/k_util/data_helper.rb +49 -39
- data/lib/k_util/version.rb +5 -5
- 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: 1cc4532082b846b6644fab95de5f16943585311d07b7dab25247418e62960e93
|
4
|
+
data.tar.gz: 59025e0e712857e698aa30242be084d0a85ab3020cea20a6f7fd49c153a2537e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90e2cdc37229d364f87dcb196a6366d1cf86e62e30634b1fff4fcc6ee5dac941396a145a052337c2b1d56a85180640c69b84c18f80c932ec667047163106cc3a
|
7
|
+
data.tar.gz: e839e60ad5abb4b00e0e46f66172255fb20976e4de99b44d80fc7d64d53fa798b5c73d82de9d6d6c9228173c19d43408c2ac81dfda91d6f471d597c67c9d30b6
|
data/Gemfile
CHANGED
data/lib/k_util/data_helper.rb
CHANGED
@@ -4,47 +4,49 @@
|
|
4
4
|
module KUtil
|
5
5
|
# Helper methods attached to the namespace for working with Data
|
6
6
|
class DataHelper
|
7
|
+
# Convert JSON string into to_open_struct but designed to work with a JSON string
|
8
|
+
#
|
9
|
+
# https://docs.ruby-lang.org/en/master/JSON.html
|
10
|
+
# rubocop:disable Naming/MethodParameterName
|
11
|
+
def parse_json(json, as: :hash)
|
12
|
+
log.block(%i[hash hash_symbolized open_struct], title: 'Help as: ?') if as == :help
|
13
|
+
|
14
|
+
case as
|
15
|
+
when :hash
|
16
|
+
JSON.parse(json)
|
17
|
+
when :hash_symbolized
|
18
|
+
JSON.parse(json, symbolize_names: true)
|
19
|
+
when :open_struct
|
20
|
+
JSON.parse(json, object_class: OpenStruct)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
alias json_parse parse_json
|
24
|
+
# rubocop:enable Naming/MethodParameterName
|
25
|
+
|
7
26
|
# Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct
|
27
|
+
#
|
8
28
|
# or an array of deep nested OpenStruct
|
9
29
|
def to_open_struct(data)
|
10
|
-
|
11
|
-
|
12
|
-
|
30
|
+
return OpenStruct.new(data.transform_values { |v| to_open_struct(v) }) if data.is_a?(Hash)
|
31
|
+
return data.map { |o| to_open_struct(o) } if data.is_a?(Array)
|
32
|
+
return to_open_struct(data.to_h) if hash_convertible?(data)
|
13
33
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
when Struct, OpenStruct
|
18
|
-
to_open_struct(data.to_h)
|
19
|
-
|
20
|
-
else
|
21
|
-
# Some primitave type: String, True/False or an ObjectStruct
|
22
|
-
data
|
23
|
-
end
|
34
|
+
# Some primitave type: String, True/False or an ObjectStruct
|
35
|
+
data
|
24
36
|
end
|
25
37
|
|
26
38
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
39
|
+
# Convert data to hash and deal with mixed data types such as Struct and OpenStruct
|
27
40
|
def to_hash(data)
|
28
|
-
#
|
29
|
-
if data.
|
30
|
-
|
31
|
-
|
41
|
+
# This nil check is only for the root object
|
42
|
+
return {} if data.nil?
|
43
|
+
|
44
|
+
return data.map { |value| hash_convertible?(value) ? to_hash(value) : value } if data.is_a?(Array)
|
32
45
|
|
33
46
|
return to_hash(data.to_h) if !data.is_a?(Hash) && data.respond_to?(:to_h)
|
34
47
|
|
35
48
|
data.each_pair.with_object({}) do |(key, value), hash|
|
36
|
-
|
37
|
-
when OpenStruct, Struct, Hash
|
38
|
-
hash[key] = to_hash(value)
|
39
|
-
when Array
|
40
|
-
# No test yet
|
41
|
-
values = value.map do |v|
|
42
|
-
v.is_a?(OpenStruct) || v.is_a?(Struct) || v.is_a?(Hash) ? to_hash(v) : v
|
43
|
-
end
|
44
|
-
hash[key] = values
|
45
|
-
else
|
46
|
-
hash[key] = value
|
47
|
-
end
|
49
|
+
hash[key] = hash_convertible?(value) ? to_hash(value) : value
|
48
50
|
end
|
49
51
|
end
|
50
52
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
@@ -55,19 +57,27 @@ module KUtil
|
|
55
57
|
value.is_a?(Symbol) ? value.to_s : value
|
56
58
|
end
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
60
|
+
def symbolize_names(hash)
|
61
|
+
hash.transform_keys(&:to_sym)
|
62
|
+
end
|
63
|
+
alias symbolize_keys symbolize_names
|
64
|
+
|
65
|
+
# Is the value a basic (aka primitive) type
|
66
|
+
def basic_type?(value)
|
67
|
+
value.is_a?(String) ||
|
68
|
+
value.is_a?(Symbol) ||
|
69
|
+
value.is_a?(FalseClass) ||
|
70
|
+
value.is_a?(TrueClass) ||
|
71
|
+
value.is_a?(Integer) ||
|
72
|
+
value.is_a?(Float)
|
73
|
+
end
|
68
74
|
|
69
75
|
# Is the value a complex container type, but not a regular class.
|
70
76
|
def hash_convertible?(value)
|
77
|
+
# Nil is a special case, it responds to :to_h but generally
|
78
|
+
# you only want to convert nil to {} in specific scenarios
|
79
|
+
return false if value.nil?
|
80
|
+
|
71
81
|
value.is_a?(Array) ||
|
72
82
|
value.is_a?(Hash) ||
|
73
83
|
value.is_a?(Struct) ||
|
data/lib/k_util/version.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module KUtil
|
4
|
-
VERSION = '0.0.
|
5
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KUtil
|
4
|
+
VERSION = '0.0.23'
|
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.
|
4
|
+
version: 0.0.23
|
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-
|
11
|
+
date: 2021-08-27 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"
|