Module: Contrast::Utils::IOUtil

Extended by:
Components::Logger::InstanceMethods
Defined in:
lib/contrast/utils/io_util.rb

Overview

Util for information about an IO

Constant Summary collapse

UNKNOWN_IO =
'unknown'

Class Method Summary collapse

Methods included from Components::Logger::InstanceMethods

cef_logger, logger

Class Method Details

.io?(object) ⇒ Boolean

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

Returns:

  • (Boolean)


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

def 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 allow lists those cases and returns false in all others.

Returns:

  • (Boolean)


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

def 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.cs__name)
  false
end