Sha256: aa3f26363f7e161e7ae2e44e11eeefcf0d17bc6d7cb42153e679f1215e57c865

Contents?: true

Size: 645 Bytes

Versions: 17

Compression:

Stored size: 645 Bytes

Contents

class Hash
  # Returns a Hash with all keys symbolized
  def deep_symbolize_keys!
    deep_transform_keys! { |key| key.to_sym rescue key } # rubocop:disable RescueModifier
  end

  def deep_transform_keys!(&block)
    _deep_transform_keys_in_object!(self, &block)
  end

  def _deep_transform_keys_in_object!(object, &block)
    case object
    when Hash
      object.keys.each do |key|
        value = object.delete(key)
        object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
      end
      object
    when Array
      object.map! { |e| _deep_transform_keys_in_object!(e, &block) }
    else
      object
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
sapience-0.2.4 lib/sapience/core_ext/hash.rb
sapience-0.2.3 lib/sapience/core_ext/hash.rb
sapience-0.2.2 lib/sapience/core_ext/hash.rb
sapience-0.2.1 lib/sapience/core_ext/hash.rb
sapience-0.2.0 lib/sapience/core_ext/hash.rb
sapience-0.1.13 lib/sapience/core_ext/hash.rb
sapience-0.1.12 lib/sapience/core_ext/hash.rb
sapience-0.1.11 lib/sapience/core_ext/hash.rb
sapience-0.1.10 lib/sapience/core_ext/hash.rb
sapience-0.1.9 lib/sapience/core_ext/hash.rb
sapience-0.1.8 lib/sapience/core_ext/hash.rb
sapience-0.1.7 lib/sapience/core_ext/hash.rb
sapience-0.1.6 lib/sapience/core_ext/hash.rb
sapience-0.1.5 lib/sapience/core_ext/hash.rb
sapience-0.1.4 lib/sapience/core_ext/hash.rb
sapience-0.1.3 lib/sapience/core_ext/hash.rb
sapience-0.1.2 lib/sapience/core_ext/hash.rb