Sha256: a556235ddbc6b8f1d62902b239d993caf8b4094c7b1f068f790ef79c899335eb

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

require 'ostruct'

module Factor
  module Common
    class DeepStruct < OpenStruct
      def initialize(hash=nil)
        @table = {}
        @hash_table = {}

        if hash
          hash.each do |k,v|
            @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
            @hash_table[k.to_sym] = v

            new_ostruct_member(k)
          end
        end
      end

      def to_h
        @hash_table
      end

      def to_s
        @hast_table.to_s
      end

      def inspect
        @hash_table.inspect
      end

      def [](idx)
        hash = marshal_dump
        hash[idx.to_sym]
      end
    end

    def self.flat_hash(h,f=[],g={})
      return g.update({ f=>h }) unless h.is_a? Hash
      h.each { |k,r| flat_hash(r,f+[k],g) }
      g
    end

    def self.simple_object_convert(item)
      if item.is_a?(Hash)
        Factor::Common::DeepStruct.new(item)
      elsif item.is_a?(Array)
        item.map do |i|
          simple_object_convert(i)
        end
      else
        item
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
factor-0.6.12 lib/common/deep_struct.rb
factor-0.6.11 lib/common/deep_struct.rb
factor-0.6.10 lib/common/deep_struct.rb
factor-0.6.9 lib/common/deep_struct.rb
factor-0.6.8 lib/common/deep_struct.rb
factor-0.6.7 lib/common/deep_struct.rb
factor-0.6.6 lib/common/deep_struct.rb
factor-0.6.5 lib/common/deep_struct.rb