Sha256: 0745889d31eed4e636036a688a84eedc1b486895f9ef28a9424b8fbaef6d549e

Contents?: true

Size: 1.41 KB

Versions: 18

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 sine 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

18 entries across 18 versions & 2 rubygems

Version Path
vagrantup-0.6.8 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.6.7 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.6.6 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.6.5 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.6.4 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.6.3 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.6.2 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.6.1 lib/vagrant/util/hash_with_indifferent_access.rb
vagrantup-0.6.0 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.8 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.7 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.6 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.5 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.4 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.3 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.2 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.1 lib/vagrant/util/hash_with_indifferent_access.rb
vagrant-0.6.0 lib/vagrant/util/hash_with_indifferent_access.rb