Sha256: 2cd4c77a3745809522aefdb42eb501bab7b0f6cfbdf5252b61572e375515c8fe

Contents?: true

Size: 991 Bytes

Versions: 11

Compression:

Stored size: 991 Bytes

Contents

module Chef::Validation
  module HashExt
    class << self
      # Return the value of the nested hash key from the given dotted path
      #
      # @example
      #
      #   nested_hash = {
      #     "deep" => {
      #       "nested" => {
      #         "hash" => :my_value
      #       }
      #     }
      #   }
      #
      #   HashExt.dig(hash, "deep/nested/hash") => :my_value
      #
      # @param [Hash] hash
      # @param [String] path
      # @param [String] separator
      #
      # @return [Object, nil]
      def dig(hash, path, separator = "/")
        return nil unless !path.nil? && !path.empty?
        return nil unless hash.respond_to?(:has_key?)
        return hash unless hash.respond_to?(:[])

        key, rest = path.split(separator, 2)
        match     = hash[key.to_s].nil? ? hash[key.to_sym] : hash[key.to_s]
        if rest.nil? or match.nil?
          match
        else
          dig(match, rest, separator)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
chef-validation-0.2.3 lib/chef/validation/ext/hash.rb
chef-validation-0.2.2 lib/chef/validation/ext/hash.rb
chef-validation-0.2.1 lib/chef/validation/ext/hash.rb
chef-validation-0.2.0 lib/chef/validation/ext/hash.rb
chef-validation-0.1.6 lib/chef/validation/ext/hash.rb
chef-validation-0.1.5 lib/chef/validation/ext/hash.rb
chef-validation-0.1.4 lib/chef/validation/ext/hash.rb
chef-validation-0.1.3 lib/chef/validation/ext/hash.rb
chef-validation-0.1.2 lib/chef/validation/ext/hash.rb
chef-validation-0.1.1 lib/chef/validation/ext/hash.rb
chef-validation-0.1.0 lib/chef/validation/ext/hash.rb