k_util 0.0.23 → 0.0.27

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: 1cc4532082b846b6644fab95de5f16943585311d07b7dab25247418e62960e93
4
- data.tar.gz: 59025e0e712857e698aa30242be084d0a85ab3020cea20a6f7fd49c153a2537e
3
+ metadata.gz: c613a872941e5e87f4e7de5f4d8572ab1347da1e107c34b26438c7691e1aca43
4
+ data.tar.gz: f1c0a56ffb60871e82b0fe4dbe9d7ae7c1d370c6f9b7e8b0dcc463d70bf4eec2
5
5
  SHA512:
6
- metadata.gz: 90e2cdc37229d364f87dcb196a6366d1cf86e62e30634b1fff4fcc6ee5dac941396a145a052337c2b1d56a85180640c69b84c18f80c932ec667047163106cc3a
7
- data.tar.gz: e839e60ad5abb4b00e0e46f66172255fb20976e4de99b44d80fc7d64d53fa798b5c73d82de9d6d6c9228173c19d43408c2ac81dfda91d6f471d597c67c9d30b6
6
+ metadata.gz: 01d75ddf3b0b7d500d3fbd3e0c3f9c6ee7421b058560cdfaff2144e2f07ab43f9f9e01dc92f68aff6001a555b6fd70f3166b26230452c5d1856faaef52fd8b98
7
+ data.tar.gz: abeb63578932c2e8330c672c3ddd62b5d2ca0661d2e038d9b9f20ec49fbea478b49b2d592750adcc7e4b882fa19d2b377fb2d7ec320b749e6860ac9bd313a1b2
@@ -1,18 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Provide data object helper functions
4
+ # include KUtil::Data::InstanceVariablesToH
4
5
  module KUtil
5
6
  module Data
6
7
  # Helper methods attached to the namespace for working with Data
7
8
  module InstanceVariablesToH
8
- def to_h
9
+ def to_h(symbolize_keys: false)
9
10
  hash = {}
10
11
  instance_variables.each do |var|
11
12
  value = instance_variable_get(var)
12
13
 
14
+ # TODO: Add deep support for symbolize_keys
13
15
  value = KUtil.data.to_hash(value) if KUtil.data.hash_convertible?(value)
14
16
 
15
- hash[var.to_s.delete('@')] = value
17
+ key = var.to_s.delete('@')
18
+ key = key.to_sym if symbolize_keys
19
+
20
+ hash[key] = value
16
21
  end
17
22
  hash
18
23
  end
@@ -6,6 +6,7 @@ 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
12
  def parse_json(json, as: :hash)
@@ -14,7 +15,7 @@ module KUtil
14
15
  case as
15
16
  when :hash
16
17
  JSON.parse(json)
17
- when :hash_symbolized
18
+ when :hash_symbolized, :symbolize_names, :symbolize_keys
18
19
  JSON.parse(json, symbolize_names: true)
19
20
  when :open_struct
20
21
  JSON.parse(json, object_class: OpenStruct)
@@ -25,6 +26,7 @@ module KUtil
25
26
 
26
27
  # Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct
27
28
  #
29
+ # KUtil.data.to_open_struct(data)
28
30
  # or an array of deep nested OpenStruct
29
31
  def to_open_struct(data)
30
32
  return OpenStruct.new(data.transform_values { |v| to_open_struct(v) }) if data.is_a?(Hash)
@@ -37,6 +39,7 @@ module KUtil
37
39
 
38
40
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
39
41
  # Convert data to hash and deal with mixed data types such as Struct and OpenStruct
42
+ # KUtil.data.to_hash(data)
40
43
  def to_hash(data)
41
44
  # This nil check is only for the root object
42
45
  return {} if data.nil?
@@ -57,10 +60,24 @@ module KUtil
57
60
  value.is_a?(Symbol) ? value.to_s : value
58
61
  end
59
62
 
60
- def symbolize_names(hash)
61
- hash.transform_keys(&:to_sym)
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
62
74
  end
63
- alias symbolize_keys symbolize_names
75
+
76
+ # def deep_symbolize_keys(hash)
77
+ # return hash if hash.nil?
78
+
79
+ # hash.transform_keys(&:to_sym)
80
+ # end
64
81
 
65
82
  # Is the value a basic (aka primitive) type
66
83
  def basic_type?(value)
@@ -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,20 @@
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 status.success?
11
+ puts "failed to run command: #{cmd}"
12
+ # bxxxinding.pry
13
+ end
14
+
15
+ raise Open3Error unless status.success?
16
+
17
+ output
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KUtil
4
- VERSION = '0.0.23'
4
+ VERSION = '0.0.27'
5
5
  end
data/lib/k_util.rb CHANGED
@@ -1,25 +1,30 @@
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'
6
8
  require 'k_util/data_helper'
7
9
  require 'k_util/file_helper'
10
+ require 'k_util/open3_helper'
8
11
 
9
12
  # Provide various helper functions
10
13
  module KUtil
11
- # raise KUtil::Error, 'Sample message'
12
- # class Error < StandardError; end
14
+ # raise KUtil::Open3Error, 'Sample message'
15
+ Open3Error = Class.new(StandardError)
13
16
 
14
17
  class << self
15
18
  attr_accessor :console
16
19
  attr_accessor :data
17
20
  attr_accessor :file
21
+ attr_accessor :open3
18
22
  end
19
23
 
20
24
  KUtil.console = KUtil::ConsoleHelper.new
21
25
  KUtil.data = KUtil::DataHelper.new
22
26
  KUtil.file = KUtil::FileHelper.new
27
+ KUtil.open3 = KUtil::Open3Helper.new
23
28
  end
24
29
 
25
30
  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.23
4
+ version: 0.0.27
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-08-27 00:00:00.000000000 Z
11
+ date: 2022-02-04 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"
@@ -43,6 +43,7 @@ files:
43
43
  - lib/k_util/data/instance_variables_to_h.rb
44
44
  - lib/k_util/data_helper.rb
45
45
  - lib/k_util/file_helper.rb
46
+ - lib/k_util/open3_helper.rb
46
47
  - lib/k_util/version.rb
47
48
  homepage: http://appydave.com/gems/k-util
48
49
  licenses:
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
67
  - !ruby/object:Gem::Version
67
68
  version: '0'
68
69
  requirements: []
69
- rubygems_version: 3.2.7
70
+ rubygems_version: 3.2.33
70
71
  signing_key:
71
72
  specification_version: 4
72
73
  summary: KUtil provides simple utility methods