Registries support merging
This commit is contained in:
parent
f1f4f276a0
commit
45879132a3
|
@ -42,6 +42,22 @@ module Vagrant
|
||||||
end
|
end
|
||||||
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
|
# Converts this registry to a hash
|
||||||
def to_hash
|
def to_hash
|
||||||
result = {}
|
result = {}
|
||||||
|
@ -51,5 +67,12 @@ module Vagrant
|
||||||
|
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def __internal_state
|
||||||
|
{
|
||||||
|
:items => @items,
|
||||||
|
:results_cache => @results_cache
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -82,4 +82,47 @@ describe Vagrant::Registry do
|
||||||
result["foo"].should == "foovalue"
|
result["foo"].should == "foovalue"
|
||||||
result["bar"].should == "barvalue"
|
result["bar"].should == "barvalue"
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue