Class: IOStreams::Delimited::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/io_streams/delimited/writer.rb

Constant Summary collapse

NOT_PRINTABLE =
Regexp.compile(/[^[:print:]]/)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_stream, delimiter: $/, encoding: UTF8_ENCODING, strip_non_printable: false) ⇒ Writer

A delimited stream writer that will write to the supplied output stream

The output stream should be binary with no text conversions performed since ‘strip_non_printable` will be applied to the binary stream before converting to UTF-8

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"

encoding:
  Force encoding to this encoding for all data being read
  Default: UTF8_ENCODING
  Set to nil to disable encoding

strip_non_printable: [true|false]
  Strip all non-printable characters read from the file
  Default: false


42
43
44
45
46
47
48
# File 'lib/io_streams/delimited/writer.rb', line 42

def initialize(output_stream, delimiter: $/, encoding: UTF8_ENCODING, strip_non_printable: false)
  @output_stream       = output_stream
  @delimiter           = delimiter.dup
  @encoding            = encoding
  @strip_non_printable = strip_non_printable
  @delimiter.force_encoding(UTF8_ENCODING) if @delimiter
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



4
5
6
# File 'lib/io_streams/delimited/writer.rb', line 4

def delimiter
  @delimiter
end

Class Method Details

.open(file_name_or_io, delimiter: $/, encoding: UTF8_ENCODING, strip_non_printable: false) ⇒ Object

Write delimited records/lines to a file or stream



7
8
9
10
11
12
13
14
15
# File 'lib/io_streams/delimited/writer.rb', line 7

def self.open(file_name_or_io, delimiter: $/, encoding: UTF8_ENCODING, strip_non_printable: false)
  if IOStreams.writer_stream?(file_name_or_io)
    yield new(file_name_or_io, delimiter: delimiter, encoding: encoding, strip_non_printable: strip_non_printable)
  else
    ::File.open(file_name_or_io, 'wb') do |io|
      yield new(io, delimiter: delimiter, encoding: encoding, strip_non_printable: strip_non_printable)
    end
  end
end

Instance Method Details

#<<(record) ⇒ Object

Write a record or line to the output stream



51
52
53
54
55
56
57
# File 'lib/io_streams/delimited/writer.rb', line 51

def <<(record)
  chunk = record.to_s
  # Strip out non-printable characters before converting to UTF-8
  chunk = chunk.gsub(NOT_PRINTABLE, '') if @strip_non_printable
  @output_stream.write((@encoding ? chunk.force_encoding(@encoding) : chunk))
  @output_stream.write(@delimiter)
end

#write(string) ⇒ Object

Write the given string to the underlying stream Note: Use of this method not recommended



61
62
63
# File 'lib/io_streams/delimited/writer.rb', line 61

def write(string)
  @output_stream.write(string)
end