Sha256: f3ddb9273e0f511c9c62f0da6c125daf2b11ea67643e9b50e8ae0691389179b2

Contents?: true

Size: 1.41 KB

Versions: 165

Compression:

Stored size: 1.41 KB

Contents

module Vagrant
  module Util
    # A hash with indifferent access. Mostly taken from Thor/Rails (thanks).
    # Normally I'm not a fan of using an indifferent access hash since Symbols
    # are basically memory leaks in Ruby, but since Vagrant is typically a quick
    # one-off binary run and it doesn't use too many hash keys where this is
    # used, the effect should be minimal.
    #
    #   hash[:foo]  #=> 'bar'
    #   hash['foo'] #=> 'bar'
    #
    class HashWithIndifferentAccess < ::Hash
      def initialize(hash={}, &block)
        super(&block)

        hash.each do |key, value|
          self[convert_key(key)] = value
        end
      end

      def [](key)
        super(convert_key(key))
      end

      def []=(key, value)
        super(convert_key(key), value)
      end

      def delete(key)
        super(convert_key(key))
      end

      def values_at(*indices)
        indices.collect { |key| self[convert_key(key)] }
      end

      def merge(other)
        dup.merge!(other)
      end

      def merge!(other)
        other.each do |key, value|
          self[convert_key(key)] = value
        end
        self
      end

      def key?(key)
        super(convert_key(key))
      end

      alias_method :include?, :key?
      alias_method :has_key?, :key?
      alias_method :member?, :key?

      protected

      def convert_key(key)
        key.is_a?(Symbol) ? key.to_s : key
      end
    end
  end
end

Version data entries

165 entries across 158 versions & 21 rubygems

Version Path
vagrantup-1.1.2 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.1.1 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.1.0 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.0.7 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.0.6 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.0.5 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.0.4 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.0.3 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.0.2 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.0.1 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-1.0.0 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.99.2 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.99.1 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.7 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.6 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.5 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.4 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.3 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.2 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.9.1 lib/vagrant/util/hash_with_indifferent_access.rb