Sha256: 2cc8c2c50c57f2bfb294e61da51d15d888217ca2177004e974ed4b24d0b85392

Contents?: true

Size: 665 Bytes

Versions: 3

Compression:

Stored size: 665 Bytes

Contents

# frozen_string_literal: true

module HashExtensions
  def to_nested
    self unless contains_dotted_key?

    keys.reduce({}) do |nested, key|
      nested.deep_merge(build_nested_object(key, self[key]))
    end
  end

  def deep_merge(hash_to_merge)
    merger = proc { |_, val_1, val_2| val_1.is_a?(Hash) && val_2.is_a?(Hash) ? val_1.merge(val_2, &merger) : val_2 }
    merge(hash_to_merge, &merger)
  end

  private

  def contains_dotted_key?
    keys.any? { |x| x.to_s.include?('.') }
  end

  def build_nested_object(key, val)
    key.to_s
       .split('.')
       .reverse
       .reduce(val) { |nested, key_part| { key_part.to_sym => nested } }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
twiglet-3.9.2 lib/hash_extensions.rb
twiglet-3.9.1 lib/hash_extensions.rb
twiglet-3.9.0 lib/hash_extensions.rb