k_util 0.0.8 → 0.0.17
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 +4 -4
 - data/lib/k_util.rb +8 -0
 - data/lib/k_util/data/instance_variables_to_h.rb +21 -0
 - data/lib/k_util/data_helper.rb +45 -4
 - data/lib/k_util/file_helper.rb +9 -0
 - data/lib/k_util/version.rb +1 -1
 - 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: b57165e4a0192dcffbfd3655e45b326bcc00db6ce32d1b72116516d32d8a5c07
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 79ee2191737f976495009f303fb7db18f0ae9e52b6b344eb642dab5ae2884b6c
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 21de731f6f1d53e8d1a28df7e1f5d80058955a517d774960f0d7362c778062cac0e9445c5d6d2cde6a88a9ce1619810dce9d5103b8a5b62d83707fce34aaabc3
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 6d062276df66896e21ffa92e07dd25d38f7951da46fcaba6ab5ced669cedd7decd669df11a7902b251589e2f17dda4bb2d7aaf61911f5c08277e20a208b6a314
         
     | 
    
        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 
     | 
    
         | 
| 
         @@ -20,3 +21,10 @@ module KUtil 
     | 
|
| 
       20 
21 
     | 
    
         
             
              KUtil.data = KUtil::DataHelper.new
         
     | 
| 
       21 
22 
     | 
    
         
             
              KUtil.file = KUtil::FileHelper.new
         
     | 
| 
       22 
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}"
         
     | 
| 
      
 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
         
     | 
    
        data/lib/k_util/data_helper.rb
    CHANGED
    
    | 
         @@ -4,7 +4,24 @@ 
     | 
|
| 
       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 
27 
     | 
    
         
             
                  case data
         
     | 
| 
         @@ -23,32 +40,56 @@ module KUtil 
     | 
|
| 
       23 
40 
     | 
    
         
             
                  end
         
     | 
| 
       24 
41 
     | 
    
         
             
                end
         
     | 
| 
       25 
42 
     | 
    
         | 
| 
       26 
     | 
    
         
            -
                # rubocop:disable Metrics/AbcSize,  Metrics/CyclomaticComplexity
         
     | 
| 
      
 43 
     | 
    
         
            +
                # rubocop:disable Metrics/AbcSize,  Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
         
     | 
| 
       27 
44 
     | 
    
         
             
                def to_hash(data)
         
     | 
| 
       28 
45 
     | 
    
         
             
                  # No test yet
         
     | 
| 
       29 
46 
     | 
    
         
             
                  if data.is_a?(Array)
         
     | 
| 
       30 
47 
     | 
    
         
             
                    return data.map { |v| v.is_a?(OpenStruct) ? to_hash(v) : v }
         
     | 
| 
       31 
48 
     | 
    
         
             
                  end
         
     | 
| 
       32 
49 
     | 
    
         | 
| 
      
 50 
     | 
    
         
            +
                  return to_hash(data.to_h) if !data.is_a?(Hash) && data.respond_to?(:to_h)
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
       33 
52 
     | 
    
         
             
                  data.each_pair.with_object({}) do |(key, value), hash|
         
     | 
| 
       34 
53 
     | 
    
         
             
                    case value
         
     | 
| 
       35 
     | 
    
         
            -
                    when OpenStruct
         
     | 
| 
      
 54 
     | 
    
         
            +
                    when OpenStruct, Struct, Hash
         
     | 
| 
       36 
55 
     | 
    
         
             
                      hash[key] = to_hash(value)
         
     | 
| 
       37 
56 
     | 
    
         
             
                    when Array
         
     | 
| 
       38 
57 
     | 
    
         
             
                      # No test yet
         
     | 
| 
       39 
     | 
    
         
            -
                      values = value.map  
     | 
| 
      
 58 
     | 
    
         
            +
                      values = value.map do |v|
         
     | 
| 
      
 59 
     | 
    
         
            +
                        v.is_a?(OpenStruct) || v.is_a?(Struct) || v.is_a?(Hash) ? to_hash(v) : v
         
     | 
| 
      
 60 
     | 
    
         
            +
                      end
         
     | 
| 
       40 
61 
     | 
    
         
             
                      hash[key] = values
         
     | 
| 
       41 
62 
     | 
    
         
             
                    else
         
     | 
| 
       42 
63 
     | 
    
         
             
                      hash[key] = value
         
     | 
| 
       43 
64 
     | 
    
         
             
                    end
         
     | 
| 
       44 
65 
     | 
    
         
             
                  end
         
     | 
| 
       45 
66 
     | 
    
         
             
                end
         
     | 
| 
       46 
     | 
    
         
            -
                # rubocop:enable Metrics/AbcSize,  Metrics/CyclomaticComplexity
         
     | 
| 
      
 67 
     | 
    
         
            +
                # rubocop:enable Metrics/AbcSize,  Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
         
     | 
| 
       47 
68 
     | 
    
         | 
| 
       48 
69 
     | 
    
         
             
                def clean_symbol(value)
         
     | 
| 
       49 
70 
     | 
    
         
             
                  return value if value.nil?
         
     | 
| 
       50 
71 
     | 
    
         | 
| 
       51 
72 
     | 
    
         
             
                  value.is_a?(Symbol) ? value.to_s : value
         
     | 
| 
       52 
73 
     | 
    
         
             
                end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                # Add if needed
         
     | 
| 
      
 76 
     | 
    
         
            +
                # # Is the value a basic (aka primitive) type
         
     | 
| 
      
 77 
     | 
    
         
            +
                # def basic_type?(value)
         
     | 
| 
      
 78 
     | 
    
         
            +
                #   value.is_a?(String) ||
         
     | 
| 
      
 79 
     | 
    
         
            +
                #     value.is_a?(Symbol) ||
         
     | 
| 
      
 80 
     | 
    
         
            +
                #     value.is_a?(FalseClass) ||
         
     | 
| 
      
 81 
     | 
    
         
            +
                #     value.is_a?(TrueClass) ||
         
     | 
| 
      
 82 
     | 
    
         
            +
                #     value.is_a?(Integer) ||
         
     | 
| 
      
 83 
     | 
    
         
            +
                #     value.is_a?(Float)
         
     | 
| 
      
 84 
     | 
    
         
            +
                # end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                # Is the value a complex container type, but not a regular class.
         
     | 
| 
      
 87 
     | 
    
         
            +
                def hash_convertible?(value)
         
     | 
| 
      
 88 
     | 
    
         
            +
                  value.is_a?(Array) ||
         
     | 
| 
      
 89 
     | 
    
         
            +
                    value.is_a?(Hash) ||
         
     | 
| 
      
 90 
     | 
    
         
            +
                    value.is_a?(Struct) ||
         
     | 
| 
      
 91 
     | 
    
         
            +
                    value.is_a?(OpenStruct) ||
         
     | 
| 
      
 92 
     | 
    
         
            +
                    value.respond_to?(:to_h)
         
     | 
| 
      
 93 
     | 
    
         
            +
                end
         
     | 
| 
       53 
94 
     | 
    
         
             
              end
         
     | 
| 
       54 
95 
     | 
    
         
             
            end
         
     | 
    
        data/lib/k_util/file_helper.rb
    CHANGED
    
    | 
         @@ -14,8 +14,17 @@ module KUtil 
     | 
|
| 
       14 
14 
     | 
    
         
             
                  end
         
     | 
| 
       15 
15 
     | 
    
         
             
                end
         
     | 
| 
       16 
16 
     | 
    
         | 
| 
      
 17 
     | 
    
         
            +
                def home?(path)
         
     | 
| 
      
 18 
     | 
    
         
            +
                  path.start_with?('~/')
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
       17 
21 
     | 
    
         
             
                def pathname_absolute?(pathname)
         
     | 
| 
       18 
22 
     | 
    
         
             
                  Pathname.new(pathname).absolute?
         
     | 
| 
       19 
23 
     | 
    
         
             
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
                alias absolute? pathname_absolute?
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                def home_or_absolute?(path)
         
     | 
| 
      
 27 
     | 
    
         
            +
                  home?(path) || absolute?(path)
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
       20 
29 
     | 
    
         
             
              end
         
     | 
| 
       21 
30 
     | 
    
         
             
            end
         
     | 
    
        data/lib/k_util/version.rb
    CHANGED
    
    
    
        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.17
         
     | 
| 
       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-07-12 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
         
     |