diff options
-rw-r--r-- | lib/puppet/indirector/request.rb | 11 | ||||
-rwxr-xr-x | spec/unit/indirector/request.rb | 15 |
2 files changed, 24 insertions, 2 deletions
diff --git a/lib/puppet/indirector/request.rb b/lib/puppet/indirector/request.rb index a5495641d..708862b28 100644 --- a/lib/puppet/indirector/request.rb +++ b/lib/puppet/indirector/request.rb @@ -3,10 +3,17 @@ require 'puppet/indirector' # Provide any attributes or functionality needed for indirected # instances. class Puppet::Indirector::Request - attr_accessor :indirection_name, :key, :method, :options + attr_accessor :indirection_name, :key, :method, :options, :instance def initialize(indirection_name, key, method, options = {}) - @indirection_name, @key, @method, @options = indirection_name, key, method, (options || {}) + @indirection_name, @method, @options = indirection_name, method, (options || {}) + + if key.is_a?(String) or key.is_a?(Symbol) + @key = key + else + @instance = key + @key = @instance.name + end raise ArgumentError, "Request options must be a hash, not %s" % @options.class unless @options.is_a?(Hash) end diff --git a/spec/unit/indirector/request.rb b/spec/unit/indirector/request.rb index ee60f600f..fd57c5297 100755 --- a/spec/unit/indirector/request.rb +++ b/spec/unit/indirector/request.rb @@ -9,6 +9,21 @@ describe Puppet::Indirector::Request do lambda { Puppet::Indirector::Request.new }.should raise_error(ArgumentError) end + it "should use provided value as the key if it is a string" do + Puppet::Indirector::Request.new(:ind, "mykey", :method).key.should == "mykey" + end + + it "should use provided value as the key if it is a symbol" do + Puppet::Indirector::Request.new(:ind, :mykey, :method).key.should == :mykey + end + + it "should use the name of the provided instance as its key if an instance is provided as the key instead of a string" do + instance = mock 'instance', :name => "mykey" + request = Puppet::Indirector::Request.new(:ind, instance, :method) + request.key.should == "mykey" + request.instance.should equal(instance) + end + it "should support options specified as a hash" do lambda { Puppet::Indirector::Request.new(:ind, :key, :method, :one => :two) }.should_not raise_error(ArgumentError) end |