Sha256: b4b861665c54ed36c85a06aae1269061eb130d631a9f7be1b51895625fcbeccb

Contents?: true

Size: 899 Bytes

Versions: 11

Compression:

Stored size: 899 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)
      @table = {}
      @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

11 entries across 11 versions & 1 rubygems

Version Path
phobos-2.0.0.pre.beta1 lib/phobos/deep_struct.rb
phobos-1.9.0 lib/phobos/deep_struct.rb
phobos-1.9.0.pre.beta3 lib/phobos/deep_struct.rb
phobos-1.9.0.pre.beta2 lib/phobos/deep_struct.rb
phobos-1.9.0.pre.beta1 lib/phobos/deep_struct.rb
phobos-1.8.3.pre.beta2 lib/phobos/deep_struct.rb
phobos-1.8.3.pre.beta1 lib/phobos/deep_struct.rb
phobos-1.8.2 lib/phobos/deep_struct.rb
phobos-1.8.2.pre.beta2 lib/phobos/deep_struct.rb
phobos-1.8.2.pre.beta1 lib/phobos/deep_struct.rb
phobos-1.8.1 lib/phobos/deep_struct.rb