Class: Cosmos::IoMultiplexer

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/io/io_multiplexer.rb

Overview

Adds IO streams and then defers to the streams when using any of the Ruby output methods such as print, puts, etc.

Direct Known Subclasses

Stderr, Stdout

Instance Method Summary collapse

Constructor Details

#initializeIoMultiplexer

Create the empty stream array



25
26
27
# File 'lib/cosmos/io/io_multiplexer.rb', line 25

def initialize
  @streams = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Forwards IO methods to all streams



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cosmos/io/io_multiplexer.rb', line 45

def method_missing(method_name, *args)
  first = true
  result = nil
  @streams.each do |stream|
    if first
      # Fortify Access Specifier Manipulation
      # We're forwarding only public methods to the stream
      result = stream.public_send(method_name, *args)
      result = self if result == stream
      first = false
    else
      # Fortify Access Specifier Manipulation
      # We're forwarding only public methods to the stream
      stream.public_send(method_name, *args)
    end
  end
  result
end

Instance Method Details

#add_stream(stream) ⇒ Object

Parameters:

  • stream (IO)

    The stream to add



71
72
73
# File 'lib/cosmos/io/io_multiplexer.rb', line 71

def add_stream(stream)
  @streams << stream unless @streams.include?(stream)
end

#remove_default_ioObject

Removes STDOUT and STDERR from the array of streams



65
66
67
68
# File 'lib/cosmos/io/io_multiplexer.rb', line 65

def remove_default_io
  @streams.delete(STDOUT)
  @streams.delete(STDERR)
end

#remove_stream(stream) ⇒ Object

Parameters:

  • stream (IO)

    The stream to remove



76
77
78
# File 'lib/cosmos/io/io_multiplexer.rb', line 76

def remove_stream(stream)
  @streams.delete(stream)
end

#write(*args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cosmos/io/io_multiplexer.rb', line 29

def write(*args)
  first = true
  result = nil
  @streams.each do |stream|
    if first
      result = stream.write(*args)
      result = self if result == stream
      first = false
    else
      stream.write(*args)
    end
  end
  result
end