Sha256: f1f9a8f404c9a09947e27b9aba2e16592624c22a5d9bd458cd208af33584afab

Contents?: true

Size: 994 Bytes

Versions: 15

Compression:

Stored size: 994 Bytes

Contents

# frozen_string_literal: true

require 'active_support/core_ext/hash/deep_merge'
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/object/deep_dup'

# Deep extensions for Hash.
class Hash
  def deep_set(*path, value:)
    raise ArgumentError, 'no path specified' if path.empty?

    if path.size == 1
      self[path[0]] = value
    else
      raise KeyError, "key not found: #{path[0]}" unless key?(path[0])
      raise ArgumentError, "value for key is not a hash: #{path[0]}" unless self.[](path[0]).is_a?(Hash)

      self.[](path[0]).deep_set(*path[1..], value: value)
    end
  end

  def deep_delete(*path)
    raise ArgumentError, 'no path specified' if path.empty?
    raise KeyError, "key not found: #{path[0]}" unless key?(path[0])

    if path.size == 1
      delete(path[0])
    else
      raise ArgumentError, "value for key is not a hash: #{path[0]}" unless self.[](path[0]).is_a?(Hash)

      self.[](path[0]).deep_delete(*path[1..])
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sqlui-0.1.84 app/deep.rb
sqlui-0.1.83 app/deep.rb
sqlui-0.1.82 app/deep.rb
sqlui-0.1.81 app/deep.rb
sqlui-0.1.80 app/deep.rb
sqlui-0.1.79 app/deep.rb
sqlui-0.1.78 app/deep.rb
sqlui-0.1.77 app/deep.rb
sqlui-0.1.76 app/deep.rb
sqlui-0.1.75 app/deep.rb
sqlui-0.1.74 app/deep.rb
sqlui-0.1.73 app/deep.rb
sqlui-0.1.72 app/deep.rb
sqlui-0.1.71 app/deep.rb
sqlui-0.1.70 app/deep.rb