Sha256: 3c1fb78789666b47e05f6df01eb5998a819bbddb65b323f874685c23a83f0616

Contents?: true

Size: 581 Bytes

Versions: 4

Compression:

Stored size: 581 Bytes

Contents

module TfgSupport
  class DeepHashAccessor

    def initialize(hash)
      @hash = hash
    end

    def [](*keys)
      target = @hash

      keys.each do |key|
        return nil unless target.respond_to?(:[])
        target = target[key]
      end
      target
    end

    def []=(*keys)
      value = keys.pop()
      set_key = keys.pop()

      target = @hash
      keys.each do |key|
        hash = target[key]
        if hash.nil?
          hash = {}
          target[key] = hash
        end
        target = hash
      end

      target[set_key] = value
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tfg_support-0.2.1 lib/tfg_support/deep_hash_accessor.rb
tfg_support-0.1.1 lib/tfg_support/deep_hash_accessor.rb
tfg_support-0.0.2 lib/tfg_support/deep_hash_accessor.rb
tfg_support-0.0.1 lib/tfg_support/deep_hash_accessor.rb