Registry is enumerable
This commit is contained in:
parent
e201d9cacf
commit
b8d40ea463
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue