Class: IOStreams::Encode::Writer
- Inherits:
-
Object
- Object
- IOStreams::Encode::Writer
- Defined in:
- lib/io_streams/encode/writer.rb
Instance Attribute Summary collapse
-
#cleaner ⇒ Object
readonly
Returns the value of attribute cleaner.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
Class Method Summary collapse
-
.open(file_name_or_io, **args) ⇒ Object
Write a line at a time to a file or stream.
Instance Method Summary collapse
-
#<<(record) ⇒ Object
Write a line to the output stream.
-
#initialize(output_stream, encoding: 'UTF-8', encode_cleaner: nil, encode_replace: nil) ⇒ Writer
constructor
A delimited stream writer that will write to the supplied output stream Written data is encoded prior to writing.
-
#write(data) ⇒ Object
Write a line to the output stream followed by the delimiter.
Constructor Details
#initialize(output_stream, encoding: 'UTF-8', encode_cleaner: nil, encode_replace: nil) ⇒ Writer
A delimited stream writer that will write to the supplied output stream Written data is encoded prior to writing.
Parameters
output_stream
The output stream that implements #write
encoding: [String|Encoding]
Encode returned data with this encoding.
'US-ASCII': Original 7 bit ASCII Format
'ASCII-8BIT': 8-bit ASCII Format
'UTF-8': UTF-8 Format
Etc.
Default: 'UTF-8'
encode_replace: [String]
The character to replace with when a character cannot be converted to the target encoding.
nil: Don't replace any invalid characters. Encoding::UndefinedConversionError is raised.
Default: nil
encode_cleaner: [nil|symbol|Proc]
Cleanse data read from the input stream.
nil: No cleansing
:printable Cleanse all non-printable characters except \r and \n
Proc/lambda Proc to call after every read to cleanse the data
Default: nil
41 42 43 44 45 46 47 |
# File 'lib/io_streams/encode/writer.rb', line 41 def initialize(output_stream, encoding: 'UTF-8', encode_cleaner: nil, encode_replace: nil) @output_stream = output_stream @cleaner = ::IOStreams::Encode::Reader.send(:extract_cleaner, encode_cleaner) @encoding = encoding.nil? || encoding.is_a?(Encoding) ? encoding : Encoding.find(encoding) @encoding_options = encode_replace.nil? ? {} : {invalid: :replace, undef: :replace, replace: encode_replace} end |
Instance Attribute Details
#cleaner ⇒ Object (readonly)
Returns the value of attribute cleaner.
4 5 6 |
# File 'lib/io_streams/encode/writer.rb', line 4 def cleaner @cleaner end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
4 5 6 |
# File 'lib/io_streams/encode/writer.rb', line 4 def encoding @encoding end |
Class Method Details
.open(file_name_or_io, **args) ⇒ Object
Write a line at a time to a file or stream
7 8 9 10 11 12 13 |
# File 'lib/io_streams/encode/writer.rb', line 7 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 end |
Instance Method Details
#<<(record) ⇒ Object
55 56 57 58 |
# File 'lib/io_streams/encode/writer.rb', line 55 def <<(record) write(record) self end |
#write(data) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/io_streams/encode/writer.rb', line 68 def write(data) return 0 if data.nil? data = data.to_s block = data.encoding == @encoding ? data : data.encode(@encoding, @encoding_options) block = @cleaner.call(block) if @cleaner @output_stream.write(block) end |