Sha256: 61adc819259a1d601af2ebf36c25e10e7a7edda50afe8ca4e2921c2b8aa37e66

Contents?: true

Size: 478 Bytes

Versions: 2

Compression:

Stored size: 478 Bytes

Contents

# frozen_string_literal: true

require 'ostruct'

class Hash
  # Stolen and refactored from https://stackoverflow.com/a/11137694
  def to_deep_ostruct
    internal_hashes = {}
    each do |key, value|
      internal_hashes[key] = value if value.is_a?(Hash)
    end

    return OpenStruct.new self if internal_hashes.empty?

    duplicate = dup
    internal_hashes.each do |key, value|
      duplicate[key] = value.to_deep_ostruct
    end
    OpenStruct.new(duplicate)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
omu-support-0.1.2 lib/omu_support/core_ext/hash.rb
omu-support-0.1.1 lib/omu_support/core_ext/hash.rb