Registry is enumerable

This commit is contained in:
Mitchell Hashimoto 2011-12-11 18:29:42 -08:00
parent e201d9cacf
commit b8d40ea463
2 changed files with 29 additions and 1 deletions

View File

@ -15,7 +15,8 @@ module Vagrant
# #
# If an action by the given name already exists then it will be # If an action by the given name already exists then it will be
# overwritten. # overwritten.
def register(key, &block) def register(key, value=nil, &block)
block = lambda { value } if value
@actions[key] = block @actions[key] = block
end end
@ -27,5 +28,12 @@ module Vagrant
return nil if !@actions.has_key?(key) return nil if !@actions.has_key?(key)
@actions[key].call @actions[key].call
end end
# Iterate over the keyspace.
def each(&block)
@actions.each do |key, _|
yield key, get(key)
end
end
end end
end end

View File

@ -7,6 +7,11 @@ describe Vagrant::Registry do
instance.get("foo").should be_nil instance.get("foo").should be_nil
end 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 it "should register an item without calling the block yet" do
expect do expect do
instance.register("foo") do instance.register("foo") do
@ -23,4 +28,19 @@ describe Vagrant::Registry do
instance.get("foo").should eql(object) instance.get("foo").should eql(object)
end 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 end