k_util 0.0.10 → 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: a1396ed8f2f544608b8847ea993a8273c133d0e27dc14f236f01f5363f101cc9
4
- data.tar.gz: a4f2a01bf25867ffa3e66dc30c2b5fe73165c57a7af80f400c4a7b6865541d9a
3
+ metadata.gz: f49e3c0ac1db1cfaf2c310205d474b0e2c24c322fad152a172bed137f7f31d2f
4
+ data.tar.gz: 2029369204a126de193b01e0219e4b37dc3e0e1745b45cdde01cdc2a159a58cf
5
5
  SHA512:
6
- metadata.gz: d1fb0d9537b8bffea7b55787e1f575bb03c1c0954f263c54a8eb1259a26e9f2efa04be13d2ebf2ea4fd5c9ffd7e1ab46f8e095dc836e3d35b18e3a4f8151d7f3
7
- data.tar.gz: 74681084346b45a0e67b88fca8a68779108e7cff25ef5d2bfa18eb680b59a686ad9b876dd1f2fb023e0766fa9a04af7865da9c958a0754d7cfa34f6526340fef
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
data/lib/k_util.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'k_util/version'
4
4
  require 'k_util/console_helper'
5
+ require 'k_util/data/instance_variables_to_h'
5
6
  require 'k_util/data_helper'
6
7
  require 'k_util/file_helper'
7
8
 
@@ -21,4 +22,9 @@ module KUtil
21
22
  KUtil.file = KUtil::FileHelper.new
22
23
  end
23
24
 
24
- puts "KUtil::Version: #{KUtil::VERSION}" if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
25
+ if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
26
+ namespace = 'KUtil::Version'
27
+ file_path = $LOADED_FEATURES.find { |f| f.include?('k_util/version') }
28
+ version = KUtil::VERSION.ljust(9)
29
+ puts "#{namespace.ljust(35)} : #{version.ljust(9)} : #{file_path}"
30
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Provide data object helper functions
4
+ module KUtil
5
+ module Data
6
+ # Helper methods attached to the namespace for working with Data
7
+ module InstanceVariablesToH
8
+ def to_h
9
+ hash = {}
10
+ instance_variables.each do |var|
11
+ value = instance_variable_get(var)
12
+
13
+ value = KUtil.data.to_hash(value) if KUtil.data.hash_convertible?(value)
14
+
15
+ hash[var.to_s.delete('@')] = value
16
+ end
17
+ hash
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,47 +4,45 @@
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 json_parse(json, as: :hash)
12
+ case as
13
+ when :hash
14
+ JSON.parse(json)
15
+ when :hash_symbolized
16
+ JSON.parse(json, symbolize_names: true)
17
+ when :open_struct
18
+ JSON.parse(json, object_class: OpenStruct)
19
+ end
20
+ end
21
+ # rubocop:enable Naming/MethodParameterName
22
+
7
23
  # Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct
24
+ #
8
25
  # or an array of deep nested OpenStruct
9
26
  def to_open_struct(data)
10
- case data
11
- when Hash
12
- 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)
13
30
 
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
31
+ # Some primitave type: String, True/False or an ObjectStruct
32
+ data
24
33
  end
25
34
 
26
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
27
37
  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
38
+ return {} if data.nil?
39
+
40
+ return data.map { |value| hash_convertible?(value) ? to_hash(value) : value } if data.is_a?(Array)
32
41
 
33
42
  return to_hash(data.to_h) if !data.is_a?(Hash) && data.respond_to?(:to_h)
34
43
 
35
44
  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) ? struct_to_hash(v) : v
43
- end
44
- hash[key] = values
45
- else
46
- hash[key] = value
47
- end
45
+ hash[key] = hash_convertible?(value) ? to_hash(value) : value
48
46
  end
49
47
  end
50
48
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -54,5 +52,25 @@ module KUtil
54
52
 
55
53
  value.is_a?(Symbol) ? value.to_s : value
56
54
  end
55
+
56
+ # Add if needed
57
+ # # Is the value a basic (aka primitive) type
58
+ # def basic_type?(value)
59
+ # value.is_a?(String) ||
60
+ # value.is_a?(Symbol) ||
61
+ # value.is_a?(FalseClass) ||
62
+ # value.is_a?(TrueClass) ||
63
+ # value.is_a?(Integer) ||
64
+ # value.is_a?(Float)
65
+ # end
66
+
67
+ # Is the value a complex container type, but not a regular class.
68
+ def hash_convertible?(value)
69
+ value.is_a?(Array) ||
70
+ value.is_a?(Hash) ||
71
+ value.is_a?(Struct) ||
72
+ value.is_a?(OpenStruct) ||
73
+ value.respond_to?(:to_h)
74
+ end
57
75
  end
58
76
  end
@@ -14,8 +14,17 @@ module KUtil
14
14
  end
15
15
  end
16
16
 
17
+ def home?(path)
18
+ path.start_with?('~/')
19
+ end
20
+
17
21
  def pathname_absolute?(pathname)
18
22
  Pathname.new(pathname).absolute?
19
23
  end
24
+ alias absolute? pathname_absolute?
25
+
26
+ def home_or_absolute?(path)
27
+ home?(path) || absolute?(path)
28
+ end
20
29
  end
21
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KUtil
4
- VERSION = '0.0.10'
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.10
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-04-01 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"
@@ -40,6 +40,7 @@ files:
40
40
  - k_util.gemspec
41
41
  - lib/k_util.rb
42
42
  - lib/k_util/console_helper.rb
43
+ - lib/k_util/data/instance_variables_to_h.rb
43
44
  - lib/k_util/data_helper.rb
44
45
  - lib/k_util/file_helper.rb
45
46
  - lib/k_util/version.rb