Allow data store to work even if file path is nil

This commit is contained in:
Mitchell Hashimoto 2012-02-25 10:41:06 -08:00
parent bd7d86f4fa
commit 7b9f64f577
2 changed files with 30 additions and 9 deletions

View File

@ -21,10 +21,11 @@ module Vagrant
def initialize(file_path) def initialize(file_path)
@logger = Log4r::Logger.new("vagrant::datastore") @logger = Log4r::Logger.new("vagrant::datastore")
if file_path
@logger.info("Created: #{file_path}") @logger.info("Created: #{file_path}")
@file_path = Pathname.new(file_path) @file_path = Pathname.new(file_path)
if @file_path.exist? if @file_path.exist?
raise Errors::DotfileIsDirectory if @file_path.directory? raise Errors::DotfileIsDirectory if @file_path.directory?
@ -35,11 +36,19 @@ module Vagrant
@logger.error("Data store contained invalid JSON. Ignoring.") @logger.error("Data store contained invalid JSON. Ignoring.")
end end
end end
else
@logger.info("No file path. In-memory data store.")
@file_path = nil
end
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
if !@file_path
raise StandardError, "In-memory data stores can't be committed."
end
clean_nil_and_empties clean_nil_and_empties
if empty? if empty?

View File

@ -64,4 +64,16 @@ describe Vagrant::DataStore do
# The file should no longer exist # The file should no longer exist
db_file.should_not be_exist db_file.should_not be_exist
end end
it "works if the DB file is nil" do
store = described_class.new(nil)
store[:foo] = "bar"
store[:foo].should == "bar"
end
it "throws an exception if attempting to commit a data store with no file" do
store = described_class.new(nil)
expect { store.commit }.
to raise_error(StandardError)
end
end end