Data store is now a hash and lazy load the local data store on the environment

This commit is contained in:
Mitchell Hashimoto 2010-09-02 11:47:19 -07:00
parent 4d87f198d7
commit 9002b22fac
4 changed files with 31 additions and 14 deletions

View File

@ -5,33 +5,24 @@ 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 class DataStore < Hash
attr_reader :file_path attr_reader :file_path
def initialize(file_path) def initialize(file_path)
@file_path = file_path @file_path = file_path
File.open(file_path, "r") do |f| File.open(file_path, "r") do |f|
@data = JSON.parse(f.read) merge!(JSON.parse(f.read))
end end
end rescue Errno::ENOENT
clear
# Returns the value associated with the `key` in the data
# store.
def [](key)
@data[key]
end
# Sets the value in the data store.
def []=(key, value)
@data[key] = value
end end
# Commits any changes to the data to disk. Even if the data # Commits any changes to the data to disk. Even if the data
# hasn't changed, it will be reserialized and written to disk. # hasn't changed, it will be reserialized and written to disk.
def commit def commit
File.open(file_path, "w") do |f| File.open(file_path, "w") do |f|
f.write(@data.to_json) f.write(to_json)
end end
end end
end end

View File

@ -151,6 +151,14 @@ module Vagrant
end end
end end
# Loads (on initial access) and reads data from the local data
# store. This file is always at the root path as the file "~/.vagrant"
# and contains a JSON dump of a hash. See {DataStore} for more
# information.
def local_data
@local_data ||= DataStore.new(dotfile_path)
end
#--------------------------------------------------------------- #---------------------------------------------------------------
# Load Methods # Load Methods
#--------------------------------------------------------------- #---------------------------------------------------------------

View File

@ -14,6 +14,10 @@ class DataStoreTest < Test::Unit::TestCase
File.delete(@db_file) File.delete(@db_file)
end end
should "just be an empty hash if file doesn't exist" do
assert @klass.new("NEvERNENVENRNE").empty?
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

View File

@ -223,6 +223,20 @@ class EnvironmentTest < Test::Unit::TestCase
end end
end end
context "local data" do
setup do
@env = mock_environment
end
should "lazy load the data store only once" do
result = mock("result")
Vagrant::DataStore.expects(:new).with(@env.dotfile_path).returns(result).once
assert_equal result, @env.local_data
assert_equal result, @env.local_data
assert_equal result, @env.local_data
end
end
context "loading" do context "loading" do
setup do setup do
@env = mock_environment @env = mock_environment