Class: IO::LikeHelpers::DelegatedIO

Inherits:
AbstractIO show all
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

BlockingIO, BufferedIO, DuplexedIO, IOWrapper, Pipeline

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractIO

#closed?, #nonblock, open

Constructor Details

#initialize(delegate, autoclose: true) ⇒ DelegatedIO

Creates a new intance of this class.

Parameters:

  • delegate (LikeHelpers::AbstractIO)

    a readable and/or writable stream

  • autoclose (Boolean) (defaults to: true)

    when ‘true` close the delegate when this stream is closed

Raises:

  • (ArgumentError)


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.

Parameters:

Returns:

  • (Proc)

    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.

Parameters:

  • autoclose (Boolean)

    delegate will be closed when ‘true`



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.

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/io/like_helpers/delegated_io.rb', line 90

def autoclose?
  assert_open
  @autoclose
end

#closenil, ...

Closes this stream.

The delegate is closed if autoclose is enabled for the stream.

Returns:

  • (nil)

    on success

  • (:wait_readable, :wait_writable)

    if the stream is non-blocking and the operation would block



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

#inspectString

Returns a string representation of this object.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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