k_util 0.0.22 → 0.0.26
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/data/instance_variables_to_h.rb +1 -0
- data/lib/k_util/data/instance_variables_to_symbolized_hash.rb +22 -0
- data/lib/k_util/data_helper.rb +27 -2
- data/lib/k_util/file_helper.rb +7 -0
- data/lib/k_util/open3_helper.rb +19 -0
- data/lib/k_util/version.rb +1 -1
- data/lib/k_util.rb +8 -2
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fea6ad816bc7b0e7d628f3726b0ba6d3f731e185405c982ff74a082e7dbaacfd
|
4
|
+
data.tar.gz: a89a98739a7ef4a2fee5ff1a7ceabeba1ff55e63238382969e99443d235e5b23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9b54e54f72d4555cc9ea62710780857f3a8c5007e9fc15599ad1ce54e509595e0517c4d6e1cb01e991457222b6d0f715541248c1d92bd31e057a7f1c25225f8
|
7
|
+
data.tar.gz: 793c00959b89a70933b72d4266dfddea2b1911d7e06f3e3fba4b4011495240e12bc90d73520aa99d7c40edb8fa4feb6996ae55969fa0e43e607e8a144800c6de
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Provide data object helper functions
|
4
|
+
# include KUtil::Data::InstanceVariablesToH
|
5
|
+
module KUtil
|
6
|
+
module Data
|
7
|
+
# Helper methods attached to the namespace for working with Data
|
8
|
+
module InstanceVariablesToSymbolizedHash
|
9
|
+
def to_h
|
10
|
+
hash = {}
|
11
|
+
instance_variables.each do |var|
|
12
|
+
value = instance_variable_get(var)
|
13
|
+
|
14
|
+
value = KUtil.data.to_hash(value) if KUtil.data.hash_convertible?(value)
|
15
|
+
|
16
|
+
hash[var.to_s.delete('@').to_sym] = value
|
17
|
+
end
|
18
|
+
hash
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/k_util/data_helper.rb
CHANGED
@@ -6,22 +6,27 @@ module KUtil
|
|
6
6
|
class DataHelper
|
7
7
|
# Convert JSON string into to_open_struct but designed to work with a JSON string
|
8
8
|
#
|
9
|
+
# KUtil.data.parse_json(json, as: :symbolize_keys)
|
9
10
|
# https://docs.ruby-lang.org/en/master/JSON.html
|
10
11
|
# rubocop:disable Naming/MethodParameterName
|
11
|
-
def
|
12
|
+
def parse_json(json, as: :hash)
|
13
|
+
log.block(%i[hash hash_symbolized open_struct], title: 'Help as: ?') if as == :help
|
14
|
+
|
12
15
|
case as
|
13
16
|
when :hash
|
14
17
|
JSON.parse(json)
|
15
|
-
when :hash_symbolized
|
18
|
+
when :hash_symbolized, :symbolize_names, :symbolize_keys
|
16
19
|
JSON.parse(json, symbolize_names: true)
|
17
20
|
when :open_struct
|
18
21
|
JSON.parse(json, object_class: OpenStruct)
|
19
22
|
end
|
20
23
|
end
|
24
|
+
alias json_parse parse_json
|
21
25
|
# rubocop:enable Naming/MethodParameterName
|
22
26
|
|
23
27
|
# Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct
|
24
28
|
#
|
29
|
+
# KUtil.data.to_open_struct(data)
|
25
30
|
# or an array of deep nested OpenStruct
|
26
31
|
def to_open_struct(data)
|
27
32
|
return OpenStruct.new(data.transform_values { |v| to_open_struct(v) }) if data.is_a?(Hash)
|
@@ -34,6 +39,7 @@ module KUtil
|
|
34
39
|
|
35
40
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
36
41
|
# Convert data to hash and deal with mixed data types such as Struct and OpenStruct
|
42
|
+
# KUtil.data.to_hash(data)
|
37
43
|
def to_hash(data)
|
38
44
|
# This nil check is only for the root object
|
39
45
|
return {} if data.nil?
|
@@ -54,6 +60,25 @@ module KUtil
|
|
54
60
|
value.is_a?(Symbol) ? value.to_s : value
|
55
61
|
end
|
56
62
|
|
63
|
+
def deep_symbolize_keys(input)
|
64
|
+
return input if input.nil?
|
65
|
+
|
66
|
+
return input unless input.is_a?(Hash)
|
67
|
+
|
68
|
+
input.each_with_object({}) do |key_value, new_hash|
|
69
|
+
key, value = key_value
|
70
|
+
value = deep_symbolize_keys(value) if value.is_a?(Hash)
|
71
|
+
value = value.map { |v| deep_symbolize_keys(v) } if value.is_a?(Array)
|
72
|
+
new_hash[key.to_sym] = value
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# def deep_symbolize_keys(hash)
|
77
|
+
# return hash if hash.nil?
|
78
|
+
|
79
|
+
# hash.transform_keys(&:to_sym)
|
80
|
+
# end
|
81
|
+
|
57
82
|
# Is the value a basic (aka primitive) type
|
58
83
|
def basic_type?(value)
|
59
84
|
value.is_a?(String) ||
|
data/lib/k_util/file_helper.rb
CHANGED
@@ -26,5 +26,12 @@ module KUtil
|
|
26
26
|
def home_or_absolute?(path)
|
27
27
|
home?(path) || absolute?(path)
|
28
28
|
end
|
29
|
+
|
30
|
+
def parse_uri(uri)
|
31
|
+
return uri if uri.is_a?(URI)
|
32
|
+
return URI.parse(uri) if uri =~ URI::ABS_URI # https://stackoverflow.com/questions/1805761/how-to-check-if-a-url-is-valid
|
33
|
+
|
34
|
+
URI.join('file:///', uri)
|
35
|
+
end
|
29
36
|
end
|
30
37
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Provide file helper functions
|
4
|
+
module KUtil
|
5
|
+
# Helper methods attached to the namespace to run Open3 commands
|
6
|
+
class Open3Helper
|
7
|
+
def capture2(cmd, **opts)
|
8
|
+
output, status = Open3.capture2(cmd, **opts)
|
9
|
+
|
10
|
+
unless success?
|
11
|
+
binding.pry
|
12
|
+
end
|
13
|
+
|
14
|
+
raise Open3Error unless status.success?
|
15
|
+
|
16
|
+
output
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/k_util/version.rb
CHANGED
data/lib/k_util.rb
CHANGED
@@ -1,25 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'open3'
|
4
|
+
|
3
5
|
require 'k_util/version'
|
4
6
|
require 'k_util/console_helper'
|
5
7
|
require 'k_util/data/instance_variables_to_h'
|
8
|
+
require 'k_util/data/instance_variables_to_symbolized_hash'
|
6
9
|
require 'k_util/data_helper'
|
7
10
|
require 'k_util/file_helper'
|
11
|
+
require 'k_util/open3_helper'
|
8
12
|
|
9
13
|
# Provide various helper functions
|
10
14
|
module KUtil
|
11
|
-
# raise KUtil::
|
12
|
-
|
15
|
+
# raise KUtil::Open3Error, 'Sample message'
|
16
|
+
Open3Error = Class.new(StandardError)
|
13
17
|
|
14
18
|
class << self
|
15
19
|
attr_accessor :console
|
16
20
|
attr_accessor :data
|
17
21
|
attr_accessor :file
|
22
|
+
attr_accessor :open3
|
18
23
|
end
|
19
24
|
|
20
25
|
KUtil.console = KUtil::ConsoleHelper.new
|
21
26
|
KUtil.data = KUtil::DataHelper.new
|
22
27
|
KUtil.file = KUtil::FileHelper.new
|
28
|
+
KUtil.open3 = KUtil::Open3Helper.new
|
23
29
|
end
|
24
30
|
|
25
31
|
if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
|
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.26
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Cruwys
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-17 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"
|
@@ -41,8 +41,10 @@ files:
|
|
41
41
|
- lib/k_util.rb
|
42
42
|
- lib/k_util/console_helper.rb
|
43
43
|
- lib/k_util/data/instance_variables_to_h.rb
|
44
|
+
- lib/k_util/data/instance_variables_to_symbolized_hash.rb
|
44
45
|
- lib/k_util/data_helper.rb
|
45
46
|
- lib/k_util/file_helper.rb
|
47
|
+
- lib/k_util/open3_helper.rb
|
46
48
|
- lib/k_util/version.rb
|
47
49
|
homepage: http://appydave.com/gems/k-util
|
48
50
|
licenses:
|
@@ -66,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
68
|
- !ruby/object:Gem::Version
|
67
69
|
version: '0'
|
68
70
|
requirements: []
|
69
|
-
rubygems_version: 3.2.
|
71
|
+
rubygems_version: 3.2.33
|
70
72
|
signing_key:
|
71
73
|
specification_version: 4
|
72
74
|
summary: KUtil provides simple utility methods
|