Module: IO::Stream::Writable
- Included in:
- Generic
- Defined in:
- lib/io/stream/writable.rb
Overview
A module providing writable stream functionality.
You must implement the ‘syswrite` method to write data to the underlying IO.
Constant Summary collapse
- ASYNC_SAFE =
{ write: true, puts: true, flush: true, }.freeze
Instance Attribute Summary collapse
-
#minimum_write_size ⇒ Object
Returns the value of attribute minimum_write_size.
Class Method Summary collapse
-
.async_safe?(method) ⇒ Boolean
Check if a method is async-safe.
Instance Method Summary collapse
-
#<<(string) ⇒ Object
Appends ‘string` to the buffer and returns self for method chaining.
-
#close_write ⇒ Object
Close the write end of the stream by flushing any remaining data.
-
#flush ⇒ Object
Flushes buffered data to the stream.
-
#initialize(minimum_write_size: MINIMUM_WRITE_SIZE, &block) ⇒ Object
Initialize writable stream functionality.
-
#puts(*arguments, separator: $/) ⇒ Object
Write arguments to the stream followed by a separator and flush immediately.
-
#write(string, flush: false) ⇒ Object
Writes ‘string` to the buffer.
Instance Attribute Details
#minimum_write_size ⇒ Object
Returns the value of attribute minimum_write_size.
40 41 42 |
# File 'lib/io/stream/writable.rb', line 40 def minimum_write_size @minimum_write_size end |
Class Method Details
.async_safe?(method) ⇒ Boolean
Check if a method is async-safe.
26 27 28 |
# File 'lib/io/stream/writable.rb', line 26 def self.async_safe?(method) ASYNC_SAFE.fetch(method, false) end |
Instance Method Details
#<<(string) ⇒ Object
Appends ‘string` to the buffer and returns self for method chaining.
71 72 73 74 75 |
# File 'lib/io/stream/writable.rb', line 71 def <<(string) write(string) return self end |
#close_write ⇒ Object
Close the write end of the stream by flushing any remaining data.
93 94 95 |
# File 'lib/io/stream/writable.rb', line 93 def close_write flush end |
#flush ⇒ Object
Flushes buffered data to the stream.
43 44 45 46 47 48 49 |
# File 'lib/io/stream/writable.rb', line 43 def flush return if @write_buffer.empty? @writing.synchronize do self.drain(@write_buffer) end end |
#initialize(minimum_write_size: MINIMUM_WRITE_SIZE, &block) ⇒ Object
Initialize writable stream functionality.
32 33 34 35 36 37 38 |
# File 'lib/io/stream/writable.rb', line 32 def initialize(minimum_write_size: MINIMUM_WRITE_SIZE, **, &block) @writing = ::Thread::Mutex.new @write_buffer = StringBuffer.new @minimum_write_size = minimum_write_size super(**, &block) if defined?(super) end |
#puts(*arguments, separator: $/) ⇒ Object
Write arguments to the stream followed by a separator and flush immediately.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/io/stream/writable.rb', line 80 def puts(*arguments, separator: $/) return if arguments.empty? @writing.synchronize do arguments.each do |argument| @write_buffer << argument << separator end self.drain(@write_buffer) end end |
#write(string, flush: false) ⇒ Object
Writes ‘string` to the buffer. When the buffer is full or #sync is true the buffer is flushed to the underlying `io`.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/io/stream/writable.rb', line 55 def write(string, flush: false) @writing.synchronize do @write_buffer << string flush |= (@write_buffer.bytesize >= @minimum_write_size) if flush self.drain(@write_buffer) end end return string.bytesize end |