Data store is a hash with indifferent access
This commit is contained in:
parent
59e1e43c74
commit
3c3c9aedc9
|
@ -5,7 +5,7 @@ module Vagrant
|
||||||
# and `[]=`. If a key is set to `nil`, then it is removed from the
|
# 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}
|
# datastore. The data store is only updated on disk when {commit}
|
||||||
# is called on the data store itself.
|
# is called on the data store itself.
|
||||||
class DataStore < Hash
|
class DataStore < Util::HashWithIndifferentAccess
|
||||||
attr_reader :file_path
|
attr_reader :file_path
|
||||||
|
|
||||||
def initialize(file_path)
|
def initialize(file_path)
|
||||||
|
@ -13,7 +13,7 @@ module Vagrant
|
||||||
return if !file_path
|
return if !file_path
|
||||||
|
|
||||||
File.open(file_path, "r") do |f|
|
File.open(file_path, "r") do |f|
|
||||||
merge!(JSON.parse(f.read, :symbolize_names => true))
|
merge!(JSON.parse(f.read))
|
||||||
end
|
end
|
||||||
rescue Errno::ENOENT
|
rescue Errno::ENOENT
|
||||||
clear
|
clear
|
||||||
|
|
|
@ -14,18 +14,22 @@ class DataStoreTest < Test::Unit::TestCase
|
||||||
File.delete(@db_file)
|
File.delete(@db_file)
|
||||||
end
|
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
|
should "just be an empty hash if file doesn't exist" do
|
||||||
assert @klass.new("NEvERNENVENRNE").empty?
|
assert @klass.new("NEvERNENVENRNE").empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
should "read the data" do
|
should "read the data" do
|
||||||
assert_equal @initial_data[:foo], @instance["foo"]
|
assert_equal @initial_data["foo"], @instance[:foo]
|
||||||
end
|
end
|
||||||
|
|
||||||
should "write the data, but not save it right away" do
|
should "write the data, but not save it right away" do
|
||||||
@instance[:foo] = "changed"
|
@instance[:foo] = "changed"
|
||||||
assert_equal "changed", @instance[:foo]
|
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
|
end
|
||||||
|
|
||||||
should "write the data if commit is called" do
|
should "write the data if commit is called" do
|
||||||
|
|
Loading…
Reference in New Issue