From b8d40ea4637cd5b0b4c88a98a0492b1300a4197a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 11 Dec 2011 18:29:42 -0800 Subject: [PATCH] Registry is enumerable --- lib/vagrant/registry.rb | 10 +++++++++- test/unit/vagrant/registry_test.rb | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/registry.rb b/lib/vagrant/registry.rb index e343b3c7c..0d608c72f 100644 --- a/lib/vagrant/registry.rb +++ b/lib/vagrant/registry.rb @@ -15,7 +15,8 @@ module Vagrant # # If an action by the given name already exists then it will be # overwritten. - def register(key, &block) + def register(key, value=nil, &block) + block = lambda { value } if value @actions[key] = block end @@ -27,5 +28,12 @@ module Vagrant return nil if !@actions.has_key?(key) @actions[key].call end + + # Iterate over the keyspace. + def each(&block) + @actions.each do |key, _| + yield key, get(key) + end + end end end diff --git a/test/unit/vagrant/registry_test.rb b/test/unit/vagrant/registry_test.rb index 1ea69802c..5c74668e6 100644 --- a/test/unit/vagrant/registry_test.rb +++ b/test/unit/vagrant/registry_test.rb @@ -7,6 +7,11 @@ describe Vagrant::Registry do instance.get("foo").should be_nil end + it "should register a simple key/value" do + instance.register("foo", "value") + instance.get("foo").should == "value" + end + it "should register an item without calling the block yet" do expect do instance.register("foo") do @@ -23,4 +28,19 @@ describe Vagrant::Registry do instance.get("foo").should eql(object) end + + it "should be enumerable" do + instance.register("foo", "foovalue") + instance.register("bar", "barvalue") + + keys = [] + values = [] + instance.each do |key, value| + keys << key + values << value + end + + keys.sort.should == ["bar", "foo"] + values.sort.should == ["barvalue", "foovalue"] + end end