Sha256: 37455866712a879f9f22c670f33195729f55de35b67478fa14dd7a00e36cfc9c

Contents?: true

Size: 893 Bytes

Versions: 13

Compression:

Stored size: 893 Bytes

Contents

# frozen_string_literal: true

# Please use this with at least the same consideration as you would when using OpenStruct.
# Right now we only use this to parse our internal configuration files. It is not meant to
# be used on incoming data.
module Phobos
  class DeepStruct < OpenStruct
    # Based on
    # https://docs.omniref.com/ruby/2.3.0/files/lib/ostruct.rb#line=88
    def initialize(hash = nil)
      super
      @hash_table = {}

      hash&.each_pair do |key, value|
        key = key.to_sym
        @table[key] = to_deep_struct(value)
        @hash_table[key] = value
      end
    end

    def to_h
      @hash_table.dup
    end
    alias to_hash to_h

    private

    def to_deep_struct(value)
      case value
      when Hash
        self.class.new(value)
      when Enumerable
        value.map { |el| to_deep_struct(el) }
      else
        value
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
phobos-2.1.6 lib/phobos/deep_struct.rb
phobos-2.1.5 lib/phobos/deep_struct.rb
phobos-2.1.4 lib/phobos/deep_struct.rb
phobos-2.1.3 lib/phobos/deep_struct.rb
phobos-2.1.2 lib/phobos/deep_struct.rb
phobos-2.1.1 lib/phobos/deep_struct.rb
phobos_temp_fork-0.0.4 lib/phobos/deep_struct.rb
phobos_temp_fork-0.0.3 lib/phobos/deep_struct.rb
phobos_temp_fork-0.0.2 lib/phobos/deep_struct.rb
phobos_temp_fork-0.0.1 lib/phobos/deep_struct.rb
phobos-2.1.0 lib/phobos/deep_struct.rb
phobos-2.0.2 lib/phobos/deep_struct.rb
phobos-2.0.1 lib/phobos/deep_struct.rb