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



23
24
25
# File 'lib/openc3/io/io_multiplexer.rb', line 23

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



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

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



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

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

#remove_default_ioObject

Removes STDOUT and STDERR from the array of streams



63
64
65
66
# File 'lib/openc3/io/io_multiplexer.rb', line 63

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

#remove_stream(stream) ⇒ Object

Parameters:

  • stream (IO)

    The stream to remove



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

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

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#write(*args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/openc3/io/io_multiplexer.rb', line 27

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