Sha256: 73972dd4671a7bab57a35e914a1beb8f18c0a0450511ab75afc3fca9cccab812

Contents?: true

Size: 897 Bytes

Versions: 14

Compression:

Stored size: 897 Bytes

Contents

# 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 = {}

      if hash
        hash.each_pair do |k, v|
          k = k.to_sym
          @table[k] = to_deep_struct(v)
          @hash_table[k] = v
        end
      end
    end

    def to_h
      @hash_table.dup
    end
    alias_method :to_hash, :to_h

    private

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
phobos-1.8.0 lib/phobos/deep_struct.rb
phobos-1.7.2 lib/phobos/deep_struct.rb
phobos-1.7.1 lib/phobos/deep_struct.rb
phobos-1.7.0 lib/phobos/deep_struct.rb
phobos-1.6.1 lib/phobos/deep_struct.rb
phobos-1.6.0 lib/phobos/deep_struct.rb
phobos-1.5.0 lib/phobos/deep_struct.rb
phobos-1.4.2 lib/phobos/deep_struct.rb
phobos-1.4.1 lib/phobos/deep_struct.rb
phobos-1.4.0 lib/phobos/deep_struct.rb
phobos-1.3.0 lib/phobos/deep_struct.rb
phobos-1.2.1 lib/phobos/deep_struct.rb
phobos-1.2.0 lib/phobos/deep_struct.rb
phobos-1.1.0 lib/phobos/deep_struct.rb