Sha256: d2a265c946f7f0da2a8f2b542dbc0981db431bc8ab969cefc95d664a092907ef

Contents?: true

Size: 792 Bytes

Versions: 5

Compression:

Stored size: 792 Bytes

Contents

# frozen_string_literal: true

module Anyway
  module Ext
    # Extend Hash through refinements
    module Hash
      refine ::Hash do
        def stringify_keys!
          keys.each do |key|
            value = delete(key)
            value.stringify_keys! if value.is_a?(::Hash)
            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]
          end
          hash[last_key] = val
        end
      end

      using self
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
anyway_config-2.3.1 lib/anyway/ext/hash.rb
anyway_config-2.3.0 lib/anyway/ext/hash.rb
anyway_config-2.2.3 lib/anyway/ext/hash.rb
anyway_config-2.2.2 lib/anyway/ext/hash.rb
anyway_config-2.1.0 lib/anyway/ext/hash.rb