From 892a624756bb85952272e3181357860abb187923 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 13 Jul 2010 20:57:14 -0700 Subject: [PATCH] Action environment hash has indifferent access --- lib/vagrant/action/environment.rb | 38 +++++++++++++++++++++++++ lib/vagrant/action/vm/halt.rb | 2 +- test/vagrant/action/environment_test.rb | 8 ++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/action/environment.rb b/lib/vagrant/action/environment.rb index cf756516f..02e5698b0 100644 --- a/lib/vagrant/action/environment.rb +++ b/lib/vagrant/action/environment.rb @@ -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 diff --git a/lib/vagrant/action/vm/halt.rb b/lib/vagrant/action/vm/halt.rb index 8bd069408..3e441fb7e 100644 --- a/lib/vagrant/action/vm/halt.rb +++ b/lib/vagrant/action/vm/halt.rb @@ -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 diff --git a/test/vagrant/action/environment_test.rb b/test/vagrant/action/environment_test.rb index 46b06326f..d512140f8 100644 --- a/test/vagrant/action/environment_test.rb +++ b/test/vagrant/action/environment_test.rb @@ -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