lib: move `Template::write_to_*` to `Streamable`
authorJean Privat <jean@pryen.org>
Thu, 13 Mar 2014 12:56:42 +0000 (08:56 -0400)
committerJean Privat <jean@pryen.org>
Fri, 14 Mar 2014 00:26:50 +0000 (20:26 -0400)
Their implementation only depends on `write_to` that is introduced
in Streamable

Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/file.nit
lib/standard/stream.nit
lib/template.nit

index 4ba20a0..951ac23 100644 (file)
@@ -205,6 +205,16 @@ end
 
 ###############################################################################
 
+redef class Streamable
+       # Like `write_to` but take care of creating the file
+       fun write_to_file(filepath: String)
+       do
+               var stream = new OFStream.open(filepath)
+               write_to(stream)
+               stream.close
+       end
+end
+
 redef class String
        # return true if a file with this names exists
        fun file_exists: Bool do return to_cstring.file_exists
index 99a31fd..5724e94 100644 (file)
@@ -102,10 +102,24 @@ end
 #
 # The point of this interface it to allow is instance to be efficenty
 # writen into a OStream without having to allocate a big String object
+#
+# ready-to-save documents usually provide this interface.
 interface Streamable
        # Write itself to a `stream`
        # The specific logic it let to the concrete subclasses
        fun write_to(stream: OStream) is abstract
+
+       # Like `write_to` but return a new String (may be quite large)
+       #
+       # This funtionnality is anectodical, since the point
+       # of streamable object to to be efficienlty written to a
+       # stream without having to allocate and concatenate strings
+       fun write_to_string: String
+       do
+               var stream = new StringOStream
+               write_to(stream)
+               return stream.to_s
+       end
 end
 
 redef class String
index e046486..9608f49 100644 (file)
@@ -200,22 +200,4 @@ class Template
 
        # Flag to avoid infinite recursivity if a template contains itself
        private var is_writing = false
-
-       # Like `write_to` but return a new String (may be quite large)
-       #
-       # Examples in this documentation use `write_to_string` but mainly for simplicity.
-       fun write_to_string: String
-       do
-               var stream = new StringOStream
-               write_to(stream)
-               return stream.to_s
-       end
-
-       # Like `write_to` but take care of creating the file
-       fun write_to_file(filepath: String)
-       do
-               var stream = new OFStream.open(filepath)
-               write_to(stream)
-               stream.close
-       end
 end