Formats can now say they support streaming (ie unformatting by
chunk).

Signed-off-by: Brice Figureau <[email protected]>
---
 lib/puppet/network/format.rb         |    4 ++++
 lib/puppet/network/format_handler.rb |   23 ++++++++++++++++++++---
 spec/unit/network/format.rb          |    4 ++++
 spec/unit/network/format_handler.rb  |   33 +++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb
index d781242..b568aa7 100644
--- a/lib/puppet/network/format.rb
+++ b/lib/puppet/network/format.rb
@@ -89,6 +89,10 @@ class Puppet::Network::Format
         suitable? and required_methods_present?(klass)
     end
 
+    def support_stream?
+        false
+    end
+
     def to_s
         "Puppet::Network::Format[%s]" % name
     end
diff --git a/lib/puppet/network/format_handler.rb 
b/lib/puppet/network/format_handler.rb
index ea8cf35..1435b99 100644
--- a/lib/puppet/network/format_handler.rb
+++ b/lib/puppet/network/format_handler.rb
@@ -23,7 +23,7 @@ module Puppet::Network::FormatHandler
             @format = format
         end
 
-        [:intern, :intern_multiple, :render, :render_multiple, :mime].each do 
|method|
+        [:intern, :intern_multiple, :render, :render_multiple, :mime, 
:support_stream?].each do |method|
             define_method(method) do |*args|
                 protect(method, args)
             end
@@ -93,12 +93,21 @@ module Puppet::Network::FormatHandler
             Puppet::Network::FormatHandler
         end
 
+        def decapsulate(format, data)
+            format = format_handler.protected_format(format)
+
+            data = data.content if !format.support_stream? and 
data.respond_to?(:stream?) and data.stream?
+            [format, data]
+        end
+
         def convert_from(format, data)
-            format_handler.protected_format(format).intern(self, data)
+            format, data = decapsulate(format, data)
+            format.intern(self, data)
         end
 
         def convert_from_multiple(format, data)
-            format_handler.protected_format(format).intern_multiple(self, data)
+            format, data = decapsulate(format, data)
+            format.intern_multiple(self, data)
         end
 
         def render_multiple(format, instances)
@@ -113,6 +122,10 @@ module Puppet::Network::FormatHandler
             Puppet::Network::FormatHandler.format(name).supported?(self)
         end
 
+        def support_stream?(name)
+            Puppet::Network::FormatHandler.format(name).support_stream?
+        end
+
         def supported_formats
             result = format_handler.formats.collect { |f| 
format_handler.format(f) }.find_all { |f| f.supported?(self) }.collect { |f| 
f.name }.sort do |a, b|
                 # It's an inverse sort -- higher weight formats go first.
@@ -164,6 +177,10 @@ module Puppet::Network::FormatHandler
         def support_format?(name)
             self.class.support_format?(name)
         end
+
+        def support_stream?(name)
+            self.class.support_stream?(name)
+        end
     end
 end
 
diff --git a/spec/unit/network/format.rb b/spec/unit/network/format.rb
index e5a6b59..feefd68 100755
--- a/spec/unit/network/format.rb
+++ b/spec/unit/network/format.rb
@@ -66,6 +66,10 @@ describe Puppet::Network::Format do
             @format.should respond_to(:supported?)
         end
 
+        it "should be able to tell it supports streaming" do
+            @format.should respond_to(:support_stream?)
+        end
+
         it "should consider a class to be supported if it has the individual 
and multiple methods for rendering and interning" do
             @format.should be_supported(FormatRenderer)
         end
diff --git a/spec/unit/network/format_handler.rb 
b/spec/unit/network/format_handler.rb
index 110effe..a5a68ea 100755
--- a/spec/unit/network/format_handler.rb
+++ b/spec/unit/network/format_handler.rb
@@ -117,6 +117,7 @@ describe Puppet::Network::FormatHandler do
             @format = mock 'format'
             @format.stubs(:supported?).returns true
             @format.stubs(:name).returns :my_format
+            @format.stubs(:support_stream?).returns false
             
Puppet::Network::FormatHandler.stubs(:format).with(:my_format).returns @format
             
Puppet::Network::FormatHandler.stubs(:mime).with("text/myformat").returns 
@format
             Puppet::Network::Format.stubs(:===).returns false
@@ -132,6 +133,15 @@ describe Puppet::Network::FormatHandler do
             FormatTester.support_format?(:my_format)
         end
 
+        it "should be able to test whether a format supports streaming" do
+            FormatTester.should respond_to(:support_stream?)
+        end
+
+        it "should use the Format to determine whether a given format supports 
streaming" do
+            @format.expects(:support_stream?)
+            FormatTester.support_stream?(:my_format)
+        end
+
         it "should be able to convert from a given format" do
             FormatTester.should respond_to(:convert_from)
         end
@@ -141,6 +151,29 @@ describe Puppet::Network::FormatHandler do
             FormatTester.convert_from(:my_format, "mydata")
         end
 
+        it "should not decapsulate the content when converting with a format 
that supports stream" do
+            data = stub_everything 'data'
+            data.expects(:content).never
+            @format.expects(:support_stream?).returns(true)
+            @format.expects(:intern).with(FormatTester, data)
+            FormatTester.convert_from(:my_format, data)
+        end
+
+        it "should not decapsulate the content when converting streamable 
data" do
+            data = stub_everything 'data', :stream? => true
+            data.expects(:content).never
+            @format.expects(:support_stream?).returns(true)
+            @format.expects(:intern).with(FormatTester, data)
+            FormatTester.convert_from(:my_format, data)
+        end
+
+        it "should decapsulate the content when converting streamable data 
with a non streamable format" do
+            data = stub_everything 'data', :stream? => true
+            data.expects(:content).returns("mydata")
+            @format.expects(:intern).with(FormatTester, "mydata")
+            FormatTester.convert_from(:my_format, data)
+        end
+
         it "should call the format-specific converter when asked to convert 
from a given format by mime-type" do
             @format.expects(:intern).with(FormatTester, "mydata")
             FormatTester.convert_from("text/myformat", "mydata")
-- 
1.6.6.1

-- 
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