diff --git a/lib/vagrant/registry.rb b/lib/vagrant/registry.rb index ef025ec0..417b4435 100644 --- a/lib/vagrant/registry.rb +++ b/lib/vagrant/registry.rb @@ -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) diff --git a/test/unit/vagrant/registry_test.rb b/test/unit/vagrant/registry_test.rb index 8d251808..a450b089 100644 --- a/test/unit/vagrant/registry_test.rb +++ b/test/unit/vagrant/registry_test.rb @@ -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")