Action environment uses new indifferent hash util

This commit is contained in:
Mitchell Hashimoto 2010-09-03 09:39:30 -07:00
parent 3c3c9aedc9
commit 0a8540996c
4 changed files with 16 additions and 49 deletions

View File

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

View File

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

View File

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

View File

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