Action environment uses new indifferent hash util
This commit is contained in:
parent
3c3c9aedc9
commit
0a8540996c
|
@ -4,7 +4,7 @@ module Vagrant
|
||||||
# to the `call` method of each action. This environment contains
|
# to the `call` method of each action. This environment contains
|
||||||
# some helper methods for accessing the environment as well
|
# some helper methods for accessing the environment as well
|
||||||
# as being a hash, to store any additional options.
|
# as being a hash, to store any additional options.
|
||||||
class Environment < Hash
|
class Environment < Util::HashWithIndifferentAccess
|
||||||
# The {Vagrant::Environment} object represented by this
|
# The {Vagrant::Environment} object represented by this
|
||||||
# action environment.
|
# action environment.
|
||||||
attr_reader :env
|
attr_reader :env
|
||||||
|
@ -50,44 +50,6 @@ module Vagrant
|
||||||
def interrupted?
|
def interrupted?
|
||||||
!!@interrupted
|
!!@interrupted
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,8 +10,8 @@ module Vagrant
|
||||||
# hash['foo'] #=> 'bar'
|
# hash['foo'] #=> 'bar'
|
||||||
#
|
#
|
||||||
class HashWithIndifferentAccess < ::Hash
|
class HashWithIndifferentAccess < ::Hash
|
||||||
def initialize(hash={})
|
def initialize(hash={}, &block)
|
||||||
super()
|
super(&block)
|
||||||
|
|
||||||
hash.each do |key, value|
|
hash.each do |key, value|
|
||||||
self[convert_key(key)] = value
|
self[convert_key(key)] = value
|
||||||
|
|
|
@ -6,6 +6,10 @@ class ActionEnvironmentTest < Test::Unit::TestCase
|
||||||
@instance = @klass.new(mock_environment)
|
@instance = @klass.new(mock_environment)
|
||||||
end
|
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
|
should "default values to those on the env" do
|
||||||
@instance.env.stubs(:key).returns("value")
|
@instance.env.stubs(:key).returns("value")
|
||||||
assert_equal "value", @instance["key"]
|
assert_equal "value", @instance["key"]
|
||||||
|
@ -24,12 +28,4 @@ class ActionEnvironmentTest < Test::Unit::TestCase
|
||||||
@instance.interrupt!
|
@instance.interrupt!
|
||||||
assert @instance.interrupted?
|
assert @instance.interrupted?
|
||||||
end
|
end
|
||||||
|
|
||||||
should "have indifferent access" do
|
|
||||||
@instance[:foo] = :bar
|
|
||||||
@instance["bar"] = :baz
|
|
||||||
|
|
||||||
assert_equal :bar, @instance["foo"]
|
|
||||||
assert_equal :baz, @instance[:bar]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,4 +27,13 @@ class HashWithIndifferentAccessUtilTest < Test::Unit::TestCase
|
||||||
assert @instance.include?(:foo)
|
assert @instance.include?(:foo)
|
||||||
assert @instance.member?(:foo)
|
assert @instance.member?(:foo)
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue