Vagrant::DataStore which will be used soon for the dotfile in the project directory
This commit is contained in:
parent
43cdcb7808
commit
4d87f198d7
|
@ -8,9 +8,10 @@ module Vagrant
|
||||||
# TODO: Move more classes over to the autoload model. We'll
|
# TODO: Move more classes over to the autoload model. We'll
|
||||||
# start small, but slowly move everything over.
|
# start small, but slowly move everything over.
|
||||||
|
|
||||||
autoload :CLI, 'vagrant/cli'
|
autoload :CLI, 'vagrant/cli'
|
||||||
autoload :Config, 'vagrant/config'
|
autoload :Config, 'vagrant/config'
|
||||||
autoload :Errors, 'vagrant/errors'
|
autoload :DataStore, 'vagrant/data_store'
|
||||||
|
autoload :Errors, 'vagrant/errors'
|
||||||
|
|
||||||
module Command
|
module Command
|
||||||
autoload :Base, 'vagrant/command/base'
|
autoload :Base, 'vagrant/command/base'
|
||||||
|
|
|
@ -18,11 +18,18 @@ module Vagrant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Converts the configuration to a raw hash.
|
||||||
|
def to_hash
|
||||||
|
instance_variables_hash.inject({}) do |acc, data|
|
||||||
|
k,v = data
|
||||||
|
v = v.to_hash if v.respond_to?(:to_hash)
|
||||||
|
acc[k] = v
|
||||||
|
acc
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def to_json(*a)
|
def to_json(*a)
|
||||||
opts = a.first if a.first.is_a?(Hash)
|
result = { 'json_class' => self.class.name }
|
||||||
opts ||= {}
|
|
||||||
result = {}
|
|
||||||
result.merge!('json_class' => self.class.name) if opts[:loadable]
|
|
||||||
result.merge(instance_variables_hash).to_json(*a)
|
result.merge(instance_variables_hash).to_json(*a)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
module Vagrant
|
||||||
|
# The Vagrant data store is a key-value store which is persisted
|
||||||
|
# as JSON in a local file which is specified in the initializer.
|
||||||
|
# The data store itself is accessed via typical hash accessors: `[]`
|
||||||
|
# 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
|
||||||
|
attr_reader :file_path
|
||||||
|
|
||||||
|
def initialize(file_path)
|
||||||
|
@file_path = file_path
|
||||||
|
|
||||||
|
File.open(file_path, "r") do |f|
|
||||||
|
@data = JSON.parse(f.read)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# Commits any changes to the data to disk. Even if the data
|
||||||
|
# hasn't changed, it will be reserialized and written to disk.
|
||||||
|
def commit
|
||||||
|
File.open(file_path, "w") do |f|
|
||||||
|
f.write(@data.to_json)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -36,7 +36,7 @@ module Vagrant
|
||||||
|
|
||||||
# Set up initial configuration
|
# Set up initial configuration
|
||||||
data = {
|
data = {
|
||||||
:config => env.config,
|
:config => env.config.to_hash,
|
||||||
:directory => env.config.vm.shared_folders["v-root"][:guestpath],
|
:directory => env.config.vm.shared_folders["v-root"][:guestpath],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,17 +21,11 @@ class ConfigBaseTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
context "converting to JSON" do
|
context "converting to JSON" do
|
||||||
should "include magic `json_class` if loadable is set to true" do
|
should "include magic `json_class`" do
|
||||||
@iv_hash = { "foo" => "bar" }
|
@iv_hash = { "foo" => "bar" }
|
||||||
@base.expects(:instance_variables_hash).returns(@iv_hash)
|
@base.expects(:instance_variables_hash).returns(@iv_hash)
|
||||||
@json = { 'json_class' => @base.class.name }.merge(@iv_hash).to_json
|
@json = { 'json_class' => @base.class.name }.merge(@iv_hash).to_json
|
||||||
assert_equal @json, @base.to_json(:loadable => true)
|
assert_equal @json, @base.to_json
|
||||||
end
|
|
||||||
|
|
||||||
should "convert instance variable hash to json" do
|
|
||||||
@iv_hash = { "foo" => "bar" }
|
|
||||||
@base.expects(:instance_variables_hash).returns(@iv_hash)
|
|
||||||
assert_equal @iv_hash.to_json, @base.to_json
|
|
||||||
end
|
end
|
||||||
|
|
||||||
should "not include env in the JSON hash" do
|
should "not include env in the JSON hash" do
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class DataStoreTest < Test::Unit::TestCase
|
||||||
|
setup do
|
||||||
|
@klass = Vagrant::DataStore
|
||||||
|
@initial_data = { "foo" => "bar" }
|
||||||
|
@db_file = File.expand_path("test/tmp/data_store_test", Vagrant.source_root)
|
||||||
|
File.open(@db_file, "w") { |f| f.write(@initial_data.to_json) }
|
||||||
|
|
||||||
|
@instance = @klass.new(@db_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
teardown do
|
||||||
|
File.delete(@db_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "read the data" do
|
||||||
|
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"]
|
||||||
|
end
|
||||||
|
|
||||||
|
should "write the data if commit is called" do
|
||||||
|
@instance["foo"] = "changed"
|
||||||
|
@instance.commit
|
||||||
|
|
||||||
|
assert_equal "changed", @klass.new(@db_file)["foo"]
|
||||||
|
end
|
||||||
|
end
|
|
@ -141,7 +141,7 @@ class ChefProvisionerTest < Test::Unit::TestCase
|
||||||
context "generating and uploading json" do
|
context "generating and uploading json" do
|
||||||
def assert_json
|
def assert_json
|
||||||
@vm.ssh.expects(:upload!).with do |json, path|
|
@vm.ssh.expects(:upload!).with do |json, path|
|
||||||
data = JSON.parse(json.read, :object_class => Hash)
|
data = JSON.parse(json.read)
|
||||||
yield data
|
yield data
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue