summaryrefslogtreecommitdiffstats
path: root/lib/puppet/resource
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-12-09 15:30:11 -0600
committerLuke Kanies <luke@madstop.com>2008-12-09 15:30:11 -0600
commitc927ce05bbd96fa9aacc8e52f8eb797403a1e433 (patch)
tree721ca37083dcf46f4f94610d8003df53450c64c1 /lib/puppet/resource
parente88746b48cfd4ce7cd9acd0baf61d9b660b460e9 (diff)
downloadpuppet-c927ce05bbd96fa9aacc8e52f8eb797403a1e433.tar.gz
puppet-c927ce05bbd96fa9aacc8e52f8eb797403a1e433.tar.xz
puppet-c927ce05bbd96fa9aacc8e52f8eb797403a1e433.zip
Renaming Puppet::ResourceReference to Puppet::Resource::Reference
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet/resource')
-rw-r--r--lib/puppet/resource/reference.rb86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/puppet/resource/reference.rb b/lib/puppet/resource/reference.rb
new file mode 100644
index 000000000..dfd6d03cb
--- /dev/null
+++ b/lib/puppet/resource/reference.rb
@@ -0,0 +1,86 @@
+#
+# Created by Luke Kanies on 2007-11-28.
+# Copyright (c) 2007. All rights reserved.
+
+require 'puppet'
+require 'puppet/resource'
+
+# A simple class to canonize how we refer to and retrieve
+# resources.
+class Puppet::Resource::Reference
+ attr_reader :type
+ attr_accessor :title, :catalog
+
+ def builtin_type?
+ builtin_type ? true : false
+ end
+
+ def initialize(type, title)
+ # This will set @type if it looks like a resource reference.
+ self.title = title
+
+ # Don't override whatever was done by setting the title.
+ self.type = type if self.type.nil?
+
+ @builtin_type = nil
+ end
+
+ # Find our resource.
+ def resolve
+ return catalog.resource(to_s) if catalog
+ return nil
+ end
+
+ # If the title has square brackets, treat it like a reference and
+ # set things appropriately; else, just set it.
+ def title=(value)
+ if value =~ /^([^\[\]]+)\[(.+)\]$/
+ self.type = $1
+ @title = $2
+ else
+ @title = value
+ end
+ end
+
+ # Canonize the type so we know it's always consistent.
+ def type=(value)
+ if value.nil? or value.to_s.downcase == "component"
+ @type = "Class"
+ else
+ # LAK:NOTE See http://snurl.com/21zf8 [groups_google_com]
+ x = @type = value.to_s.split("::").collect { |s| s.capitalize }.join("::")
+ end
+ end
+
+ # Convert to the reference format that TransObject uses. Yay backward
+ # compatibility.
+ def to_trans_ref
+ # We have to return different cases to provide backward compatibility
+ # from 0.24.x to 0.23.x.
+ if builtin_type?
+ return [type.to_s.downcase, title.to_s]
+ else
+ return [type.to_s, title.to_s]
+ end
+ end
+
+ # Convert to the standard way of referring to resources.
+ def to_s
+ "%s[%s]" % [@type, @title]
+ end
+
+ private
+
+ def builtin_type
+ if @builtin_type.nil?
+ if @type =~ /::/
+ @builtin_type = false
+ elsif klass = Puppet::Type.type(@type.to_s.downcase)
+ @builtin_type = true
+ else
+ @builtin_type = false
+ end
+ end
+ @builtin_type
+ end
+end