Registries support merging

This commit is contained in:
Mitchell Hashimoto 2013-01-13 12:58:48 -08:00
parent f1f4f276a0
commit 45879132a3
2 changed files with 66 additions and 0 deletions

View File

@ -42,6 +42,22 @@ module Vagrant
end
end
# Merge one registry with another and return a completely new
# registry. Note that the result cache is completely busted, so
# any gets on the new registry will result in a cache miss.
def merge(other)
self.class.new.tap do |result|
result.merge!(self)
result.merge!(other)
end
end
# Like #{merge} but merges into self.
def merge!(other)
@items.merge!(other.__internal_state[:items])
self
end
# Converts this registry to a hash
def to_hash
result = {}
@ -51,5 +67,12 @@ module Vagrant
result
end
def __internal_state
{
:items => @items,
:results_cache => @results_cache
}
end
end
end

View File

@ -82,4 +82,47 @@ describe Vagrant::Registry do
result["foo"].should == "foovalue"
result["bar"].should == "barvalue"
end
describe "merging" do
it "should merge in another registry" do
one = described_class.new
two = described_class.new
one.register("foo") { raise "BOOM!" }
two.register("bar") { raise "BAM!" }
three = one.merge(two)
expect { three["foo"] }.to raise_error("BOOM!")
expect { three["bar"] }.to raise_error("BAM!")
end
it "should NOT merge in the cache" do
one = described_class.new
two = described_class.new
one.register("foo") { [] }
one["foo"] << 1
two.register("bar") { [] }
two["bar"] << 2
three = one.merge(two)
three["foo"].should == []
three["bar"].should == []
end
end
describe "merge!" do
it "should merge into self" do
one = described_class.new
two = described_class.new
one.register("foo") { "foo" }
two.register("bar") { "bar" }
one.merge!(two)
one["foo"].should == "foo"
one["bar"].should == "bar"
end
end
end