From fae30756e48184dfc8238dcfe80b843f981b6070 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Thu, 11 Dec 2008 12:31:01 -0600 Subject: Simplifying the initialization interface for References You previously had to call new(nil, "Foo[bar]") if you just had the resource reference as a string. Now you can call new("Foo[bar]"), but the old behaviour works, too. Signed-off-by: Luke Kanies --- lib/puppet/resource/reference.rb | 15 ++++++++++----- spec/unit/resource/reference.rb | 10 ++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/puppet/resource/reference.rb b/lib/puppet/resource/reference.rb index d254254aa..d17b3e558 100644 --- a/lib/puppet/resource/reference.rb +++ b/lib/puppet/resource/reference.rb @@ -21,12 +21,17 @@ class Puppet::Resource::Reference builtin_type ? true : false end - def initialize(type, title) - # This will set @type if it looks like a resource reference. - self.title = title + def initialize(argtype, argtitle = nil) + if argtitle.nil? + self.title = argtype + raise ArgumentError, "No title provided and title '%s' is not a valid resource reference" % argtype if self.title == argtype + else + # This will set @type if it looks like a resource reference. + self.title = argtitle - # Don't override whatever was done by setting the title. - self.type = type if self.type.nil? + # Don't override whatever was done by setting the title. + self.type ||= argtype + end @builtin_type = nil end diff --git a/spec/unit/resource/reference.rb b/spec/unit/resource/reference.rb index 001aa0395..fa2d87bd5 100755 --- a/spec/unit/resource/reference.rb +++ b/spec/unit/resource/reference.rb @@ -47,6 +47,16 @@ describe Puppet::Resource::Reference do ref.title.should =="baz[yay]" end + it "should interpret the type as a reference and assign appropriately if the title is nil and the type contains square brackets" do + ref = Puppet::Resource::Reference.new("foo::bar[baz]") + ref.type.should == "Foo::Bar" + ref.title.should =="baz" + end + + it "should fail if the title is nil and the type is not a valid resource reference string" do + lambda { Puppet::Resource::Reference.new("foo") }.should raise_error(ArgumentError) + end + it "should be considered builtin if an existing resource type matches the type" do Puppet::Resource::Reference.new("file", "/f").should be_builtin_type end -- cgit