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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b521dc46323b33d082574de08feba5ef83f167b0feea433d902217e62c934557
4
- data.tar.gz: 6f4f9aca566de81776cdbe6496772034a13f14a973297f738a9a6ecf6856f3eb
3
+ metadata.gz: 1cc4532082b846b6644fab95de5f16943585311d07b7dab25247418e62960e93
4
+ data.tar.gz: 59025e0e712857e698aa30242be084d0a85ab3020cea20a6f7fd49c153a2537e
5
5
  SHA512:
6
- metadata.gz: cf2a72c8bcb86ca6ac4d1e3b286150bf175c5e95018eef603d32fecd010b490e3f266944642cbe6ad9f66dbedcccf30d8c7b4824ff6feb661ad35f257969965e
7
- data.tar.gz: da68fc44fe300cb6130a4e25a31a57431f47d01152683b76f35d5b16a04d34e121d9d66bcc8971d5e1f70e6fd99d588c20b19f815dc1aa9130b951189a88c8a7
6
+ metadata.gz: 90e2cdc37229d364f87dcb196a6366d1cf86e62e30634b1fff4fcc6ee5dac941396a145a052337c2b1d56a85180640c69b84c18f80c932ec667047163106cc3a
7
+ data.tar.gz: e839e60ad5abb4b00e0e46f66172255fb20976e4de99b44d80fc7d64d53fa798b5c73d82de9d6d6c9228173c19d43408c2ac81dfda91d6f471d597c67c9d30b6
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
@@ -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
- case data
11
- when Hash
12
- OpenStruct.new(data.transform_values { |v| to_open_struct(v) })
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
- when Array
15
- data.map { |o| to_open_struct(o) }
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
- # No test yet
29
- if data.is_a?(Array)
30
- return data.map { |v| v.is_a?(OpenStruct) ? to_hash(v) : v }
31
- end
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
- case value
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
- # Add if needed
59
- # # Is the value a basic (aka primitive) type
60
- # def basic_type?(value)
61
- # value.is_a?(String) ||
62
- # value.is_a?(Symbol) ||
63
- # value.is_a?(FalseClass) ||
64
- # value.is_a?(TrueClass) ||
65
- # value.is_a?(Integer) ||
66
- # value.is_a?(Float)
67
- # end
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) ||
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module KUtil
4
- VERSION = '0.0.15'
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.15
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-04-19 00:00:00.000000000 Z
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"