diff --git a/lib/vagrant/action/environment.rb b/lib/vagrant/action/environment.rb index d10ce2152..41f2ceb6e 100644 --- a/lib/vagrant/action/environment.rb +++ b/lib/vagrant/action/environment.rb @@ -4,7 +4,7 @@ module Vagrant # to the `call` method of each action. This environment contains # some helper methods for accessing the environment as well # as being a hash, to store any additional options. - class Environment < Hash + class Environment < Util::HashWithIndifferentAccess # The {Vagrant::Environment} object represented by this # action environment. attr_reader :env @@ -50,44 +50,6 @@ module Vagrant def interrupted? !!@interrupted end - - #----------------------------------------------------------------- - # Hash with indifferent access - #----------------------------------------------------------------- - 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 has_key?(key) - super(convert_key(key)) - end - - def convert_key(key) - key.is_a?(Symbol) ? key.to_s : key - end end end end diff --git a/lib/vagrant/util/hash_with_indifferent_access.rb b/lib/vagrant/util/hash_with_indifferent_access.rb index 176cfb9d6..be7b86114 100644 --- a/lib/vagrant/util/hash_with_indifferent_access.rb +++ b/lib/vagrant/util/hash_with_indifferent_access.rb @@ -10,8 +10,8 @@ module Vagrant # hash['foo'] #=> 'bar' # class HashWithIndifferentAccess < ::Hash - def initialize(hash={}) - super() + def initialize(hash={}, &block) + super(&block) hash.each do |key, value| self[convert_key(key)] = value diff --git a/test/vagrant/action/environment_test.rb b/test/vagrant/action/environment_test.rb index eb21ec665..59e5b3173 100644 --- a/test/vagrant/action/environment_test.rb +++ b/test/vagrant/action/environment_test.rb @@ -6,6 +6,10 @@ class ActionEnvironmentTest < Test::Unit::TestCase @instance = @klass.new(mock_environment) end + should "be a hash with indifferent access" do + assert @instance.is_a?(Vagrant::Util::HashWithIndifferentAccess) + end + should "default values to those on the env" do @instance.env.stubs(:key).returns("value") assert_equal "value", @instance["key"] @@ -24,12 +28,4 @@ class ActionEnvironmentTest < Test::Unit::TestCase @instance.interrupt! assert @instance.interrupted? end - - should "have indifferent access" do - @instance[:foo] = :bar - @instance["bar"] = :baz - - assert_equal :bar, @instance["foo"] - assert_equal :baz, @instance[:bar] - end end diff --git a/test/vagrant/util/hash_with_indifferent_access_test.rb b/test/vagrant/util/hash_with_indifferent_access_test.rb index acf94dfd6..b4a8058e8 100644 --- a/test/vagrant/util/hash_with_indifferent_access_test.rb +++ b/test/vagrant/util/hash_with_indifferent_access_test.rb @@ -27,4 +27,13 @@ class HashWithIndifferentAccessUtilTest < Test::Unit::TestCase assert @instance.include?(:foo) assert @instance.member?(:foo) end + + should "forward up block to Hash if given to initializer" do + instance = @klass.new do |h,k| + h[k] = "foo" + end + + assert_equal "foo", instance[:foo] + assert_equal "foo", instance["foo"] + end end