Class: IO::LikeHelpers::DelegatedIO
- Inherits:
-
AbstractIO
- Object
- AbstractIO
- IO::LikeHelpers::DelegatedIO
- Defined in:
- lib/io/like_helpers/delegated_io.rb
Overview
This class implements AbstractIO by delegating most methods to a delegate stream. Use this class to implement streams that filter or mutate data sent through them.
Direct Known Subclasses
Class Method Summary collapse
-
.create_finalizer(delegate) ⇒ Proc
A proc to be used as a fializer that calls #close on ‘delegate` when an instance of this class it garbage collected.
Instance Method Summary collapse
-
#advise(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.advise(*args, **kwargs, &b)` after asserting that the stream is open.
-
#autoclose=(autoclose) ⇒ Object
Sets whether or not to close the delegate when #close is called.
-
#autoclose? ⇒ Boolean
Returns ‘true` if the delegate would be closed when #close is called and `false` otherwise.
-
#close ⇒ nil, ...
Closes this stream.
-
#close_on_exec=(value) ⇒ Object
Calls ‘delegate.close_on_exec = value` after asserting that the stream is open.
-
#close_on_exec?(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.close_on_exec?(*args, **kwargs, &b)` after asserting that the stream is open.
-
#fcntl(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.fcntl(*args, **kwargs, &b)` after asserting that the stream is open.
-
#fdatasync(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.fdatasync(*args, **kwargs, &b)` after asserting that the stream is open.
-
#fileno(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.fileno(*args, **kwargs, &b)` after asserting that the stream is open.
-
#fsync(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.fsync(*args, **kwargs, &b)` after asserting that the stream is open.
-
#initialize(delegate, autoclose: true) ⇒ DelegatedIO
constructor
Creates a new intance of this class.
-
#inspect ⇒ String
A string representation of this object.
-
#ioctl(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.ioctl(*args, **kwargs, &b)` after asserting that the stream is open.
-
#nonblock=(value) ⇒ Object
Calls ‘delegate.nonblock = value` after asserting that the stream is open.
-
#nonblock?(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.nonblock?(*args, **kwargs, &b)` after asserting that the stream is open.
-
#nread(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.nread(*args, **kwargs, &b)` after asserting that the stream is readable.
-
#path(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.path(*args, **kwargs, &b)` after asserting that the stream is open.
-
#pid(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.pid(*args, **kwargs, &b)` after asserting that the stream is open.
-
#pread(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.read(*args, **kwargs, &b)` after asserting that the stream is readable.
-
#pwrite(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.write(*args, **kwargs, &b)` after asserting that the stream is writable.
-
#read(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.read(*args, **kwargs, &b)` after asserting that the stream is readable.
-
#readable? ⇒ Boolean
Returns ‘true` if the stream is readable and `false` otherwise.
-
#ready?(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.ready?(*args, **kwargs, &b)` after asserting that the stream is open.
-
#seek(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.seek(*args, **kwargs, &b)` after asserting that the stream is open.
-
#stat(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.stat(*args, **kwargs, &b)` after asserting that the stream is open.
-
#to_io(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.to_io(*args, **kwargs, &b)` after asserting that the stream is open.
-
#tty?(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.tty?(*args, **kwargs, &b)` after asserting that the stream is open.
-
#wait(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.wait(*args, **kwargs, &b)` after asserting that the stream is open.
-
#writable? ⇒ Boolean
Returns ‘true` if the stream is writable and `false` otherwise.
-
#write(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.write(*args, **kwargs, &b)` after asserting that the stream is writable.
Methods inherited from AbstractIO
Constructor Details
#initialize(delegate, autoclose: true) ⇒ DelegatedIO
Creates a new intance of this class.
66 67 68 69 70 71 72 |
# File 'lib/io/like_helpers/delegated_io.rb', line 66 def initialize(delegate, autoclose: true) raise ArgumentError, 'delegate cannot be nil' if delegate.nil? super() @delegate = delegate self.autoclose = autoclose end |
Class Method Details
.create_finalizer(delegate) ⇒ Proc
Returns a proc to be used as a fializer that calls #close on ‘delegate` when an instance of this class it garbage collected.
56 57 58 |
# File 'lib/io/like_helpers/delegated_io.rb', line 56 def self.create_finalizer(delegate) proc { |id| delegate.close } end |
Instance Method Details
#advise(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.advise(*args, **kwargs, &b)` after asserting that the stream is open.
160 |
# File 'lib/io/like_helpers/delegated_io.rb', line 160 delegate :advise |
#autoclose=(autoclose) ⇒ Object
Sets whether or not to close the delegate when #close is called.
78 79 80 81 82 83 |
# File 'lib/io/like_helpers/delegated_io.rb', line 78 def autoclose=(autoclose) assert_open @autoclose = autoclose ? true : false @autoclose ? enable_finalizer : disable_finalizer autoclose end |
#autoclose? ⇒ Boolean
Returns ‘true` if the delegate would be closed when #close is called and `false` otherwise.
90 91 92 93 |
# File 'lib/io/like_helpers/delegated_io.rb', line 90 def autoclose? assert_open @autoclose end |
#close ⇒ nil, ...
Closes this stream.
The delegate is closed if autoclose is enabled for the stream.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/io/like_helpers/delegated_io.rb', line 103 def close return nil if closed? begin result = delegate.close if @autoclose ensure # Complete the closing process if the delegate closed normally or an # exception was raised. unless Symbol === result disable_finalizer result = super end end result end |
#close_on_exec=(value) ⇒ Object
Calls ‘delegate.close_on_exec = value` after asserting that the stream is open.
150 |
# File 'lib/io/like_helpers/delegated_io.rb', line 150 delegate :close_on_exec= |
#close_on_exec?(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.close_on_exec?(*args, **kwargs, &b)` after asserting that the stream is open.
161 |
# File 'lib/io/like_helpers/delegated_io.rb', line 161 delegate :close_on_exec? |
#fcntl(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.fcntl(*args, **kwargs, &b)` after asserting that the stream is open.
162 |
# File 'lib/io/like_helpers/delegated_io.rb', line 162 delegate :fcntl |
#fdatasync(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.fdatasync(*args, **kwargs, &b)` after asserting that the stream is open.
163 |
# File 'lib/io/like_helpers/delegated_io.rb', line 163 delegate :fdatasync |
#fileno(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.fileno(*args, **kwargs, &b)` after asserting that the stream is open.
164 |
# File 'lib/io/like_helpers/delegated_io.rb', line 164 delegate :fileno |
#fsync(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.fsync(*args, **kwargs, &b)` after asserting that the stream is open.
165 |
# File 'lib/io/like_helpers/delegated_io.rb', line 165 delegate :fsync |
#inspect ⇒ String
Returns a string representation of this object.
122 123 124 |
# File 'lib/io/like_helpers/delegated_io.rb', line 122 def inspect "<#{self.class}:#{delegate.inspect}#{' (closed)' if closed?}>" end |
#ioctl(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.ioctl(*args, **kwargs, &b)` after asserting that the stream is open.
166 |
# File 'lib/io/like_helpers/delegated_io.rb', line 166 delegate :ioctl |
#nonblock=(value) ⇒ Object
Calls ‘delegate.nonblock = value` after asserting that the stream is open.
155 |
# File 'lib/io/like_helpers/delegated_io.rb', line 155 delegate :nonblock= |
#nonblock?(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.nonblock?(*args, **kwargs, &b)` after asserting that the stream is open.
167 |
# File 'lib/io/like_helpers/delegated_io.rb', line 167 delegate :nonblock? |
#nread(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.nread(*args, **kwargs, &b)` after asserting that the stream is readable. Calls `delegate.nread(*args, **kwargs, &b)` after asserting that the stream is open.
190 |
# File 'lib/io/like_helpers/delegated_io.rb', line 190 delegate :nread, assert: :readable |
#path(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.path(*args, **kwargs, &b)` after asserting that the stream is open.
168 |
# File 'lib/io/like_helpers/delegated_io.rb', line 168 delegate :path |
#pid(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.pid(*args, **kwargs, &b)` after asserting that the stream is open.
169 |
# File 'lib/io/like_helpers/delegated_io.rb', line 169 delegate :pid |
#pread(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.read(*args, **kwargs, &b)` after asserting that the stream is readable. Calls `delegate.pread(*args, **kwargs, &b)` after asserting that the stream is open.
180 |
# File 'lib/io/like_helpers/delegated_io.rb', line 180 delegate :pread, assert: :readable |
#pwrite(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.write(*args, **kwargs, &b)` after asserting that the stream is writable. Calls `delegate.pwrite(*args, **kwargs, &b)` after asserting that the stream is open.
195 |
# File 'lib/io/like_helpers/delegated_io.rb', line 195 delegate :pwrite, assert: :writable |
#read(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.read(*args, **kwargs, &b)` after asserting that the stream is readable. Calls `delegate.read(*args, **kwargs, &b)` after asserting that the stream is open.
185 |
# File 'lib/io/like_helpers/delegated_io.rb', line 185 delegate :read, assert: :readable |
#readable? ⇒ Boolean
Returns ‘true` if the stream is readable and `false` otherwise.
130 131 132 133 134 |
# File 'lib/io/like_helpers/delegated_io.rb', line 130 def readable? return false if closed? return @readable if defined?(@readable) && ! @readable.nil? @readable = delegate.readable? end |
#ready?(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.ready?(*args, **kwargs, &b)` after asserting that the stream is open.
170 |
# File 'lib/io/like_helpers/delegated_io.rb', line 170 delegate :ready? |
#seek(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.seek(*args, **kwargs, &b)` after asserting that the stream is open.
171 |
# File 'lib/io/like_helpers/delegated_io.rb', line 171 delegate :seek |
#stat(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.stat(*args, **kwargs, &b)` after asserting that the stream is open.
172 |
# File 'lib/io/like_helpers/delegated_io.rb', line 172 delegate :stat |
#to_io(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.to_io(*args, **kwargs, &b)` after asserting that the stream is open.
173 |
# File 'lib/io/like_helpers/delegated_io.rb', line 173 delegate :to_io |
#tty?(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.tty?(*args, **kwargs, &b)` after asserting that the stream is open.
174 |
# File 'lib/io/like_helpers/delegated_io.rb', line 174 delegate :tty? |
#wait(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.wait(*args, **kwargs, &b)` after asserting that the stream is open.
175 |
# File 'lib/io/like_helpers/delegated_io.rb', line 175 delegate :wait |
#writable? ⇒ Boolean
Returns ‘true` if the stream is writable and `false` otherwise.
140 141 142 143 144 |
# File 'lib/io/like_helpers/delegated_io.rb', line 140 def writable? return false if closed? return @writable if defined?(@writable) && ! @writable.nil? @writable = delegate.writable? end |
#write(*args, **kwargs, &b) ⇒ Object
Calls ‘delegate.write(*args, **kwargs, &b)` after asserting that the stream is writable. Calls `delegate.write(*args, **kwargs, &b)` after asserting that the stream is open.
200 |
# File 'lib/io/like_helpers/delegated_io.rb', line 200 delegate :write, assert: :writable |