Registry will now cache result values.

This is actually required so that we can do things like this
in plugins:

Vagrant.actions[:up].insert(Foo, Bar)
master
Mitchell Hashimoto 2012-01-28 17:30:24 -08:00
parent 88ba3a3619
commit 912e4974db
2 changed files with 23 additions and 2 deletions

View File

@ -6,6 +6,7 @@ module Vagrant
class Registry
def initialize
@actions = {}
@results_cache = {}
end
# Register a callable by key.
@ -26,8 +27,10 @@ module Vagrant
# action stack.
def get(key)
return nil if !@actions.has_key?(key)
@actions[key].call
return @results_cache[key] if @results_cache.has_key?(key)
@results_cache[key] = @actions[key].call
end
alias :[] :get
# Iterate over the keyspace.
def each(&block)

View File

@ -20,7 +20,7 @@ describe Vagrant::Registry do
end.to_not raise_error
end
it "should call and return the result of a block when asking for the ite" do
it "should call and return the result of a block when asking for the item" do
object = Object.new
instance.register("foo") do
object
@ -29,6 +29,24 @@ describe Vagrant::Registry do
instance.get("foo").should eql(object)
end
it "should be able to get the item with []" do
object = Object.new
instance.register("foo") { object }
instance["foo"].should eql(object)
end
it "should cache the result of the item so they can be modified" do
# Make the proc generate a NEW array each time
instance.register("foo") { [] }
# Test that modifying the result modifies the actual cached
# value. This verifies we're caching.
instance.get("foo").should == []
instance.get("foo") << "value"
instance.get("foo").should == ["value"]
end
it "should be enumerable" do
instance.register("foo", "foovalue")
instance.register("bar", "barvalue")