Sha256: 9229e665bf78e3ba1ddf290a0975e34ba77d1b2a90ed5ca33a8968799a3ad84a

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module IOStreams
  module Line
    class Writer < IOStreams::Writer
      attr_reader :delimiter

      # 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.
      # To change the output encoding, use IOStreams::Encode::Writer.
      #
      # Parameters
      #   output_stream
      #     The output stream that implements #write
      #
      #   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: $/)
        super(output_stream)
        @delimiter = delimiter
      end

      # Write a line to the output stream
      #
      # Example:
      #   IOStreams.line_writer('a.txt') do |stream|
      #     stream << 'first line' << 'second line'
      #   end
      def <<(data)
        write(data)
        self
      end

      # Write a line to the output stream followed by the delimiter.
      # Returns [Integer] the number of bytes written.
      #
      # Example:
      #   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)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iostreams-1.0.0.beta lib/io_streams/line/writer.rb