k_util 0.0.7 → 0.0.15

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: 88c89c4c9e613a825a162a59b8b4ea546d747f223d4d93ce93f26b3c734d49c2
4
- data.tar.gz: 94b16c31d2149012c947e85a677f36b33f559cd914636269f8b5ce9bb9bc56bb
3
+ metadata.gz: b521dc46323b33d082574de08feba5ef83f167b0feea433d902217e62c934557
4
+ data.tar.gz: 6f4f9aca566de81776cdbe6496772034a13f14a973297f738a9a6ecf6856f3eb
5
5
  SHA512:
6
- metadata.gz: ca99065afbe8883d234c30a4cef1b56c69d2be2fd48860918158f0f1df8bc40cd77cc23b0a72a459ab765a1bb1c7b944f1ef6bc67b1c85a840a6df8c8a13cf24
7
- data.tar.gz: fef92543798f378b9aedb86f1db84619b2a82fb0246f13ac1e99932def8e37a4bd391aa24a5411c3726aaea807b37cb22f33115fc47cc40c0ae404b67d8e4b2b
6
+ metadata.gz: cf2a72c8bcb86ca6ac4d1e3b286150bf175c5e95018eef603d32fecd010b490e3f266944642cbe6ad9f66dbedcccf30d8c7b4824ff6feb661ad35f257969965e
7
+ data.tar.gz: da68fc44fe300cb6130a4e25a31a57431f47d01152683b76f35d5b16a04d34e121d9d66bcc8971d5e1f70e6fd99d588c20b19f815dc1aa9130b951189a88c8a7
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
 
@@ -9,4 +10,21 @@ require 'k_util/file_helper'
9
10
  module KUtil
10
11
  # raise KUtil::Error, 'Sample message'
11
12
  # class Error < StandardError; end
13
+
14
+ class << self
15
+ attr_accessor :console
16
+ attr_accessor :data
17
+ attr_accessor :file
18
+ end
19
+
20
+ KUtil.console = KUtil::ConsoleHelper.new
21
+ KUtil.data = KUtil::DataHelper.new
22
+ KUtil.file = KUtil::FileHelper.new
23
+ end
24
+
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}"
12
30
  end
@@ -11,14 +11,12 @@ module KUtil
11
11
  # Convert a hash into a deep OpenStruct or array an array
12
12
  # of objects into an array of OpenStruct
13
13
  # Generate hyper link encoded string for the console
14
- def self.file_hyperlink(text, file)
14
+ def file_hyperlink(text, file)
15
15
  "\u001b]8;;file://#{file}\u0007#{text}\u001b]8;;\u0007"
16
16
  end
17
17
 
18
- def self.hyperlink(text, link)
18
+ def hyperlink(text, link)
19
19
  "\u001b]8;;#{link}\u0007#{text}\u001b]8;;\u0007"
20
20
  end
21
21
  end
22
22
  end
23
-
24
- KUtil.console = KUtil::ConsoleHelper
@@ -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
@@ -2,15 +2,11 @@
2
2
 
3
3
  # Provide data object helper functions
4
4
  module KUtil
5
- class << self
6
- attr_accessor :data
7
- end
8
-
9
5
  # Helper methods attached to the namespace for working with Data
10
6
  class DataHelper
11
7
  # Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct
12
8
  # or an array of deep nested OpenStruct
13
- def self.to_open_struct(data)
9
+ def to_open_struct(data)
14
10
  case data
15
11
  when Hash
16
12
  OpenStruct.new(data.transform_values { |v| to_open_struct(v) })
@@ -27,34 +23,56 @@ module KUtil
27
23
  end
28
24
  end
29
25
 
30
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
31
- def self.to_hash(data)
26
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
27
+ def to_hash(data)
32
28
  # No test yet
33
29
  if data.is_a?(Array)
34
30
  return data.map { |v| v.is_a?(OpenStruct) ? to_hash(v) : v }
35
31
  end
36
32
 
33
+ return to_hash(data.to_h) if !data.is_a?(Hash) && data.respond_to?(:to_h)
34
+
37
35
  data.each_pair.with_object({}) do |(key, value), hash|
38
36
  case value
39
- when OpenStruct
37
+ when OpenStruct, Struct, Hash
40
38
  hash[key] = to_hash(value)
41
39
  when Array
42
40
  # No test yet
43
- values = value.map { |v| v.is_a?(OpenStruct) ? to_hash(v) : v }
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
44
  hash[key] = values
45
45
  else
46
46
  hash[key] = value
47
47
  end
48
48
  end
49
49
  end
50
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
50
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
51
51
 
52
- def self.clean_symbol(value)
52
+ def clean_symbol(value)
53
53
  return value if value.nil?
54
54
 
55
55
  value.is_a?(Symbol) ? value.to_s : value
56
56
  end
57
+
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
68
+
69
+ # Is the value a complex container type, but not a regular class.
70
+ def hash_convertible?(value)
71
+ value.is_a?(Array) ||
72
+ value.is_a?(Hash) ||
73
+ value.is_a?(Struct) ||
74
+ value.is_a?(OpenStruct) ||
75
+ value.respond_to?(:to_h)
76
+ end
57
77
  end
58
78
  end
59
-
60
- KUtil.data = KUtil::DataHelper
@@ -2,13 +2,9 @@
2
2
 
3
3
  # Provide file helper functions
4
4
  module KUtil
5
- class << self
6
- attr_accessor :file
7
- end
8
-
9
5
  # Helper methods attached to the namespace for working with Files
10
6
  class FileHelper
11
- def self.expand_path(filename, base_path = nil)
7
+ def expand_path(filename, base_path = nil)
12
8
  if pathname_absolute?(filename)
13
9
  filename
14
10
  elsif filename.start_with?('~/')
@@ -18,10 +14,17 @@ module KUtil
18
14
  end
19
15
  end
20
16
 
21
- def self.pathname_absolute?(pathname)
17
+ def home?(path)
18
+ path.start_with?('~/')
19
+ end
20
+
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
26
-
27
- KUtil.file = KUtil::FileHelper
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module KUtil
4
- VERSION = '0.0.7'
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module KUtil
4
+ VERSION = '0.0.15'
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.7
4
+ version: 0.0.15
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-03-31 00:00:00.000000000 Z
11
+ date: 2021-04-19 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