Class: Contrast::Utils::IOUtil

Inherits:
Object
  • Object
show all
Includes:
Components::Interface
Defined in:
lib/contrast/utils/io_util.rb

Overview

Util for information about an IO

Class Method Summary collapse

Methods included from Components::Interface

included

Class Method Details

.io?(object) ⇒ Boolean

A class is IO if it is a decedent or delegate of IO

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
# File 'lib/contrast/utils/io_util.rb', line 40

def self.io? object
  return true if object.is_a?(IO)

  # DelegateClass, which is a Delegator, defines __getobj__ as a way to
  # get the object that the class wraps around (delegates to)
  return false unless object.is_a?(Delegator)

  object.__getobj__.is_a?(IO)
end

.should_rewind?(potential_io) ⇒ Boolean

We’re only going to call rewind on things that we believe are safe to call it on. This method white lists those cases and returns false in all others.

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
# File 'lib/contrast/utils/io_util.rb', line 17

def self.should_rewind? potential_io
  return true if potential_io.is_a?(StringIO)
  return false unless io?(potential_io)

  should_rewind_io?(potential_io)
rescue StandardError => e
  logger.debug('Encountered an issue determining if rewindable', e, module: potential_io.cs__class.name)
  false
end

.should_rewind_io?(io) ⇒ Boolean

IO cannot be used with streams such as pipes, ttys, and sockets.

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# File 'lib/contrast/utils/io_util.rb', line 28

def self.should_rewind_io? io
  return false if io.tty?

  status = io.stat
  return false unless status
  return false if status.pipe?
  return false if status.socket?

  true
end