Sha256: b2386a91f8014657f84b9a19696686e39451456cd0af93910f35057b1cdc7b75

Contents?: true

Size: 862 Bytes

Versions: 3

Compression:

Stored size: 862 Bytes

Contents

# frozen_string_literal: true

# Extend Hash through refinements
module Runger::Ext::Hash
  refine ::Hash do
    def stringify_keys!
      keys.each do |key|
        value = delete(key)
        self[key.to_s] = value
      end

      self
    end

    def bury(val, *path)
      raise(ArgumentError, 'No path specified') if path.empty?
      raise(ArgumentError, 'Path cannot contain nil') if path.compact.size != path.size

      last_key = path.pop
      hash =
        path.reduce(self) do |hash, k|
          hash[k] = {} unless hash.key?(k) && hash[k].is_a?(::Hash)
          hash[k]
        end

      hash[last_key] = val
    end

    def deep_transform_keys(&block)
      each_with_object({}) do |(key, value), result|
        result[yield(key)] = value.is_a?(::Hash) ? value.deep_transform_keys(&block) : value
      end
    end
  end

  using self
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
runger_config-5.2.0 lib/runger/ext/hash.rb
runger_config-5.1.0 lib/runger/ext/hash.rb
runger_config-5.0.0 lib/runger/ext/hash.rb