lib/io_streams/line/writer.rb in iostreams-0.20.3 vs lib/io_streams/line/writer.rb in iostreams-1.0.0.beta
- old
+ new
@@ -1,17 +1,16 @@
module IOStreams
module Line
- class Writer
+ class Writer < IOStreams::Writer
attr_reader :delimiter
- # Write a line at a time to a file or stream
- def self.open(file_name_or_io, **args)
- if file_name_or_io.is_a?(String)
- IOStreams::File::Writer.open(file_name_or_io) { |io| yield new(io, **args) }
- else
- yield new(file_name_or_io, **args)
- end
+ # Write a line at a time to a stream.
+ def self.stream(output_stream, original_file_name: nil, **args, &block)
+ # Pass-through if already a line writer
+ return block.call(output_stream) if output_stream.is_a?(self.class)
+
+ yield new(output_stream, **args)
end
# A delimited stream writer that will write to the supplied output stream.
#
# The output stream will have the encoding of data written to it.
@@ -24,12 +23,12 @@
# delimiter: [String]
# Add the specified delimiter after every record when writing it
# to the output stream
# Default: OS Specific. Linux: "\n"
def initialize(output_stream, delimiter: $/)
- @output_stream = output_stream
- @delimiter = delimiter
+ super(output_stream)
+ @delimiter = delimiter
end
# Write a line to the output stream
#
# Example:
@@ -48,10 +47,10 @@
# IOStreams.line_writer('a.txt') do |stream|
# count = stream.write('first line')
# puts "Wrote #{count} bytes to the output file, including the delimiter"
# end
def write(data)
- @output_stream.write(data.to_s + delimiter)
+ output_stream.write(data.to_s + delimiter)
end
end
end
end