Data store is a hash with indifferent access

This commit is contained in:
Mitchell Hashimoto 2010-09-03 09:35:07 -07:00
parent 59e1e43c74
commit 3c3c9aedc9
2 changed files with 8 additions and 4 deletions

View File

@ -5,7 +5,7 @@ module Vagrant
# and `[]=`. If a key is set to `nil`, then it is removed from the
# datastore. The data store is only updated on disk when {commit}
# is called on the data store itself.
class DataStore < Hash
class DataStore < Util::HashWithIndifferentAccess
attr_reader :file_path
def initialize(file_path)
@ -13,7 +13,7 @@ module Vagrant
return if !file_path
File.open(file_path, "r") do |f|
merge!(JSON.parse(f.read, :symbolize_names => true))
merge!(JSON.parse(f.read))
end
rescue Errno::ENOENT
clear

View File

@ -14,18 +14,22 @@ class DataStoreTest < Test::Unit::TestCase
File.delete(@db_file)
end
should "be a hash with indifferent access" do
assert @instance.is_a?(Vagrant::Util::HashWithIndifferentAccess)
end
should "just be an empty hash if file doesn't exist" do
assert @klass.new("NEvERNENVENRNE").empty?
end
should "read the data" do
assert_equal @initial_data[:foo], @instance["foo"]
assert_equal @initial_data["foo"], @instance[:foo]
end
should "write the data, but not save it right away" do
@instance[:foo] = "changed"
assert_equal "changed", @instance[:foo]
assert_equal @initial_data[:foo], @klass.new(@db_file)["foo"]
assert_equal @initial_data["foo"], @klass.new(@db_file)["foo"]
end
should "write the data if commit is called" do