k_util 0.0.12 → 0.0.22

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: 3860a3f695361bef8207528e54cfd32dcd870f1947e5ad8548619ff4fb4225ba
4
- data.tar.gz: 37abdfd64ba5001844737429e4786d02c606816803571c44082f133bf44982ca
3
+ metadata.gz: 615b0deb70283841ac540e1af6a4153dcf7fdb6b1eb85f4710119edd7c662507
4
+ data.tar.gz: 0cf049101ab9e7ea4e76943c2e19e69a550d4dca78f31b2a70e6e51f6ca13a5c
5
5
  SHA512:
6
- metadata.gz: 8e293e90a1a83f31ceabdc7ae1c1324859cfa8e20bd186c05a6c78a3b603f2b48b99d74d67408f06d930d751f3eadb6e8cfbb8e7cf1c133faa458523ffb73484
7
- data.tar.gz: 2d7f7251ed0c427b47addbd9ef062477abb8a4072eddb3829157e24eb56ad65fe86705046cb28481927a7ae5fcdb387807d474bddbcc3dfabb2b7f95f2972360
6
+ metadata.gz: 5d85601784381afbea1385b067cd646ddb72675a463638b2b4b611fff3657d1924ca4dba2f0ede9fa1bd17090481c54814a31d24f92190a47dfa735512677615
7
+ data.tar.gz: 0a15d56a6099b870971367b360fe6a2780c30fcedc1b9b759c6e2cccfe698a4386d2f85af143057c3f4f7df7f3b918ca4b73ba46241f2b6503eeb58756b3e0d8
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
 
@@ -25,5 +26,5 @@ if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
25
26
  namespace = 'KUtil::Version'
26
27
  file_path = $LOADED_FEATURES.find { |f| f.include?('k_util/version') }
27
28
  version = KUtil::VERSION.ljust(9)
28
- puts "#{namespace.ljust(40)} : #{version.ljust(9)} : #{file_path}"
29
+ puts "#{namespace.ljust(35)} : #{version.ljust(9)} : #{file_path}"
29
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,46 @@
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) })
13
-
14
- when Array
15
- data.map { |o| to_open_struct(o) }
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)
16
30
 
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
+ # This nil check is only for the root object
39
+ return {} if data.nil?
40
+
41
+ return data.map { |value| hash_convertible?(value) ? to_hash(value) : value } if data.is_a?(Array)
32
42
 
33
43
  return to_hash(data.to_h) if !data.is_a?(Hash) && data.respond_to?(:to_h)
34
44
 
35
45
  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
46
+ hash[key] = hash_convertible?(value) ? to_hash(value) : value
48
47
  end
49
48
  end
50
49
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -54,5 +53,28 @@ module KUtil
54
53
 
55
54
  value.is_a?(Symbol) ? value.to_s : value
56
55
  end
56
+
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
+ # Nil is a special case, it responds to :to_h but generally
70
+ # you only want to convert nil to {} in specific scenarios
71
+ return false if value.nil?
72
+
73
+ value.is_a?(Array) ||
74
+ value.is_a?(Hash) ||
75
+ value.is_a?(Struct) ||
76
+ value.is_a?(OpenStruct) ||
77
+ value.respond_to?(:to_h)
78
+ end
57
79
  end
58
80
  end
@@ -14,12 +14,17 @@ module KUtil
14
14
  end
15
15
  end
16
16
 
17
- def has_home?(path)
17
+ def home?(path)
18
18
  path.start_with?('~/')
19
19
  end
20
20
 
21
21
  def pathname_absolute?(pathname)
22
22
  Pathname.new(pathname).absolute?
23
23
  end
24
+ alias absolute? pathname_absolute?
25
+
26
+ def home_or_absolute?(path)
27
+ home?(path) || absolute?(path)
28
+ end
24
29
  end
25
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KUtil
4
- VERSION = '0.0.12'
4
+ VERSION = '0.0.22'
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.12
4
+ version: 0.0.22
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-03 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