k_util 0.0.7 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/k_util.rb +18 -0
- data/lib/k_util/console_helper.rb +2 -4
- data/lib/k_util/data/instance_variables_to_h.rb +21 -0
- data/lib/k_util/data_helper.rb +31 -13
- data/lib/k_util/file_helper.rb +11 -8
- data/lib/k_util/version.rb +5 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b521dc46323b33d082574de08feba5ef83f167b0feea433d902217e62c934557
|
4
|
+
data.tar.gz: 6f4f9aca566de81776cdbe6496772034a13f14a973297f738a9a6ecf6856f3eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
14
|
+
def file_hyperlink(text, file)
|
15
15
|
"\u001b]8;;file://#{file}\u0007#{text}\u001b]8;;\u0007"
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
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
|
data/lib/k_util/data_helper.rb
CHANGED
@@ -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
|
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
|
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
|
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
|
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
|
data/lib/k_util/file_helper.rb
CHANGED
@@ -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
|
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
|
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
|
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.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.
|
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-
|
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
|