Sha256: 5ef1737551c984a538ed9a039eb8d7a2f41b9d7b28762704ceb2dd4160784fcf

Contents?: true

Size: 1.12 KB

Versions: 19

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Facter
  module Utils
    # Sort nested hash.
    def self.sort_hash_by_key(hash, recursive = true, &block)
      hash.keys.sort(&block).each_with_object(hash.class.new) do |key, seed|
        seed[key] = hash[key]
        seed[key] = sort_hash_by_key(seed[key], true, &block) if recursive && seed[key].is_a?(Hash)

        seed
      end
    end

    def self.split_user_query(user_query)
      queries = user_query.split('.')
      queries.map! { |query| query =~ /^[0-9]+$/ ? query.to_i : query }
    end

    def self.deep_stringify_keys(object)
      case object
      when Hash
        object.each_with_object({}) do |(key, value), result|
          result[key.to_s] = deep_stringify_keys(value)
        end
      when Array
        object.map { |e| deep_stringify_keys(e) }
      else
        object
      end
    end

    def self.try_to_bool(value)
      case value.to_s
      when 'true'
        true
      when 'false'
        false
      else
        value
      end
    end

    def self.try_to_int(value)
      Integer(value)
    rescue ArgumentError, TypeError
      value
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
facter-4.3.1 lib/facter/util/utils.rb
facter-4.3.0 lib/facter/util/utils.rb
facter-4.2.14 lib/facter/util/utils.rb
facter-4.2.13 lib/facter/util/utils.rb
facter-4.2.12 lib/facter/util/utils.rb
facter-4.2.11 lib/facter/util/utils.rb
facter-4.2.10 lib/facter/util/utils.rb
facter-4.2.9 lib/facter/util/utils.rb
facter-4.2.8 lib/facter/util/utils.rb
facter-4.2.7 lib/facter/util/utils.rb
facter-4.2.6 lib/facter/util/utils.rb
facter-4.2.5 lib/facter/util/utils.rb
facter-4.2.4 lib/facter/util/utils.rb
facter-4.2.3 lib/facter/util/utils.rb
facter-4.2.2 lib/facter/util/utils.rb
facter-4.2.1 lib/facter/util/utils.rb
facter-4.2.0 lib/facter/util/utils.rb
facter-4.1.1 lib/facter/util/utils.rb
facter-4.1.0 lib/facter/util/utils.rb