(I can't get git-format-patch to work, so in the meantime...)

commit 2a680d3c52c9fa69b9995331fa727987e30d38ea
Author: Luke Kanies <[EMAIL PROTECTED]>
Date:   Mon May 26 00:24:57 2008 -0500

     Adding a class for using templates directly within resources
     (i.e., client-side templates).  This would really only be used
     for composite resources that pass the results of the template
     on to generated resources.

diff --git a/CHANGELOG b/CHANGELOG
index 7edb7bd..bde3649 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,8 @@
+    Adding a ResourceTemplate class for using templates directly
+    within resources (i.e., client-side templates).  This would really
+    only be used for composite resources that pass the results of the
+    template on to generated resources.
+
      Fixing transaction support for prefetching generated resources.

      Adding support for settings within the existing Facter provider  
confines.
diff --git a/lib/puppet/util/resource_template.rb b/lib/puppet/util/ 
resource_template.rb
new file mode 100644
index 0000000..f850780
--- /dev/null
+++ b/lib/puppet/util/resource_template.rb
@@ -0,0 +1,38 @@
+require 'puppet/util'
+require 'puppet/util/logging'
+require 'erb'
+
+# A template wrapper that evaluates a template in the
+# context of a resource, allowing the resource attributes
+# to be looked up from within the template.
+#  This provides functionality essentially equivalent to
+# the language's template() function.  You pass your file
+# path and the resource you want to use into the initialization
+# method, then call result() on the instance, and you get back
+# a chunk of text.
+#  The resource's parameters are available as instance variables
+# (as opposed to the language, where we use a method_missing trick).
+class Puppet::Util::ResourceTemplate
+    include Puppet::Util::Logging
+
+    def evaluate
+        set_resource_variables
+        ERB.new(File.read(@file), 0, "-").result(binding)
+    end
+
+    def initialize(file, resource)
+        raise ArgumentError, "Template %s does not exist" % file  
unless FileTest.exist?(file)
+        @file = file
+        @resource = resource
+    end
+
+    private
+
+    def set_resource_variables
+        @resource.to_hash.each do |param, value|
+            var = "@#{param.to_s}"
+            instance_variable_set(var, value)
+        end
+    end
+end
+
diff --git a/spec/unit/util/resource_template.rb b/spec/unit/util/ 
resource_template.rb
new file mode 100755
index 0000000..b4d529e
--- /dev/null
+++ b/spec/unit/util/resource_template.rb
@@ -0,0 +1,58 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/util/resource_template'
+
+describe Puppet::Util::ResourceTemplate do
+    describe "when initializing" do
+        it "should fail if the template does not exist" do
+            FileTest.expects(:exist?).with("/my/template").returns  
false
+            lambda { Puppet::Util::ResourceTemplate.new("/my/ 
template", mock('resource')) }.should raise_error(ArgumentError)
+        end
+
+        it "should not create the ERB template" do
+            ERB.expects(:new).never
+            FileTest.expects(:exist?).with("/my/template").returns true
+            Puppet::Util::ResourceTemplate.new("/my/template",  
mock('resource'))
+        end
+    end
+
+    describe "when evaluating" do
+        before do
+            FileTest.stubs(:exist?).returns true
+            File.stubs(:read).returns "eh"
+
+            @template = stub 'template', :result => nil
+            ERB.stubs(:new).returns @template
+
+            @resource = mock 'resource'
+            @wrapper = Puppet::Util::ResourceTemplate.new("/my/ 
template", @resource)
+        end
+
+        it "should set all of the resource's parameters as instance  
variables" do
+            @resource.expects(:to_hash).returns(:one => "uno", :two  
=> "dos")
+            @template.expects(:result).with do |bind|
+                eval("@one", bind) == "uno" and eval("@two", bind) ==  
"dos"
+            end
+            @wrapper.evaluate
+        end
+
+        it "should create a template instance with the contents of  
the file" do
+            File.expects(:read).with("/my/template").returns "yay"
+            ERB.expects(:new).with("yay", 0, "-").returns(@template)
+
+            @wrapper.stubs :set_resource_variables
+
+            @wrapper.evaluate
+        end
+
+        it "should return the result of the template" do
+            @wrapper.stubs :set_resource_variables
+
+            @wrapper.expects(:binding).returns "mybinding"
+            @template.expects(:result).with("mybinding").returns  
"myresult"
+            @wrapper.evaluate.should == "myresult"
+        end
+    end
+end


-- 
People are more violently opposed to fur than leather because it is
safer to harrass rich women than motorcycle gangs.
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to