Action environment hash has indifferent access

This commit is contained in:
Mitchell Hashimoto 2010-07-13 20:57:14 -07:00
parent a0fa3755b5
commit 892a624756
3 changed files with 47 additions and 1 deletions

View File

@ -49,6 +49,44 @@ module Vagrant
def error?
!error.nil?
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

@ -11,7 +11,7 @@ module Vagrant
def call(env)
if env["vm"].vm.running?
if !env[:force]
if !env["force"]
catch_action_exception(env) { env["vm"].system.halt }
return if env.error?
end

View File

@ -28,4 +28,12 @@ class ActionEnvironmentTest < Test::Unit::TestCase
@instance.error!(:key)
assert @instance.error?
end
should "have indifferent access" do
@instance[:foo] = :bar
@instance["bar"] = :baz
assert_equal :bar, @instance["foo"]
assert_equal :baz, @instance[:bar]
end
end