Add #length and #size methods to Registry

This commit is contained in:
Seth Vargo 2014-10-23 11:17:23 -04:00
parent 60a8472891
commit bc4bbb9fc0
2 changed files with 31 additions and 1 deletions

View File

@ -34,7 +34,7 @@ module Vagrant
def has_key?(key)
@items.has_key?(key)
end
# Returns an array populated with the keys of this object.
#
# @return [Array]
@ -49,6 +49,14 @@ module Vagrant
end
end
# Return the number of elements in this registry.
#
# @return [Fixnum]
def length
@items.keys.length
end
alias_method :size, :length
# Merge one registry with another and return a completely new
# registry. Note that the result cache is completely busted, so
# any gets on the new registry will result in a cache miss.

View File

@ -90,6 +90,28 @@ describe Vagrant::Registry do
expect(result["bar"]).to eq("barvalue")
end
describe "#length" do
it "should return 0 when the registry is empty" do
expect(instance.length).to eq(0)
end
it "should return the number of items in the registry" do
instance.register("foo") { }
instance.register("bar") { }
expect(instance.length).to eq(2)
end
end
describe "#size" do
it "should be an alias to #length" do
size = described_class.instance_method(:size)
length = described_class.instance_method(:length)
expect(size).to eq(length)
end
end
describe "merging" do
it "should merge in another registry" do
one = described_class.new