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

View File

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