Class: OpenC3::IoMultiplexer

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/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



28
29
30
# File 'lib/openc3/io/io_multiplexer.rb', line 28

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openc3/io/io_multiplexer.rb', line 48

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



74
75
76
# File 'lib/openc3/io/io_multiplexer.rb', line 74

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

#remove_default_ioObject

Removes STDOUT and STDERR from the array of streams



68
69
70
71
# File 'lib/openc3/io/io_multiplexer.rb', line 68

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

#remove_stream(stream) ⇒ Object

Parameters:

  • stream (IO)

    The stream to remove



79
80
81
# File 'lib/openc3/io/io_multiplexer.rb', line 79

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

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/openc3/io/io_multiplexer.rb', line 84

def respond_to_missing?(method, *)
  STDOUT.public_methods.include?(method) || super
end

#write(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openc3/io/io_multiplexer.rb', line 32

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