Don't allow registry to be given value in non-block form

This commit is contained in:
Mitchell Hashimoto 2012-11-03 20:46:24 -07:00
parent 6df6f6764f
commit 6ca671e2b8
2 changed files with 25 additions and 24 deletions

View File

@ -2,33 +2,29 @@ module Vagrant
# Register components in a single location that can be queried.
#
# This allows certain components (such as guest systems, configuration
# pieces, etc.) to be registered and queried.
# pieces, etc.) to be registered and queried, lazily.
class Registry
def initialize
@actions = {}
@items = {}
@results_cache = {}
end
# Register a callable by key.
# Register a key with a lazy-loaded value.
#
# The callable should be given in a block which will be lazily evaluated
# when the action is needed.
#
# If an action by the given name already exists then it will be
# overwritten.
def register(key, value=nil, &block)
block = lambda { value } if value
@actions[key] = block
# If a key with the given name already exists, it is overwritten.
def register(key, &block)
raise ArgumentError, "block required" if !block_given?
@items[key] = block
end
# Get an action by the given key.
# Get a value by the given key.
#
# This will evaluate the block given to `register` and return the resulting
# action stack.
# This will evaluate the block given to `register` and return the
# resulting value.
def get(key)
return nil if !@actions.has_key?(key)
return nil if !@items.has_key?(key)
return @results_cache[key] if @results_cache.has_key?(key)
@results_cache[key] = @actions[key].call
@results_cache[key] = @items[key].call
end
alias :[] :get
@ -36,12 +32,12 @@ module Vagrant
#
# @return [Boolean]
def has_key?(key)
@actions.has_key?(key)
@items.has_key?(key)
end
# Iterate over the keyspace.
def each(&block)
@actions.each do |key, _|
@items.each do |key, _|
yield key, get(key)
end
end

View File

@ -8,7 +8,7 @@ describe Vagrant::Registry do
end
it "should register a simple key/value" do
instance.register("foo", "value")
instance.register("foo") { "value" }
instance.get("foo").should == "value"
end
@ -20,6 +20,11 @@ describe Vagrant::Registry do
end.to_not raise_error
end
it "should raise an error if no block is given" do
expect { instance.register("foo") }.
to raise_error(ArgumentError)
end
it "should call and return the result of a block when asking for the item" do
object = Object.new
instance.register("foo") do
@ -48,14 +53,14 @@ describe Vagrant::Registry do
end
it "should be able to check if a key exists" do
instance.register("foo", "bar")
instance.register("foo") { "bar" }
instance.should have_key("foo")
instance.should_not have_key("bar")
end
it "should be enumerable" do
instance.register("foo", "foovalue")
instance.register("bar", "barvalue")
instance.register("foo") { "foovalue" }
instance.register("bar") { "barvalue" }
keys = []
values = []
@ -69,8 +74,8 @@ describe Vagrant::Registry do
end
it "should be able to convert to a hash" do
instance.register("foo", "foovalue")
instance.register("bar", "barvalue")
instance.register("foo") { "foovalue" }
instance.register("bar") { "barvalue" }
result = instance.to_hash
result.should be_a(Hash)