Class: YleTf::System::IOHandlers

Inherits:
Object
  • Object
show all
Defined in:
lib/yle_tf/system/io_handlers.rb

Constant Summary collapse

BLOCK_SIZE =
1024

Class Method Summary collapse

Class Method Details

.closeObject

Returns a lambda that just closes the IO



55
56
57
# File 'lib/yle_tf/system/io_handlers.rb', line 55

def self.close
  ->(io, *) { io.close }
end

.console_inputObject

Returns a lambda that pipes STDIN to the IO



60
61
62
# File 'lib/yle_tf/system/io_handlers.rb', line 60

def self.console_input
  io_input(STDIN)
end

.console_outputObject

Returns a lambda that pipes IO’s output to STDOUT



65
66
67
# File 'lib/yle_tf/system/io_handlers.rb', line 65

def self.console_output
  io_output(STDOUT)
end

.copy_data(source, target, **opts) ⇒ Object

Reads all data from the source IO and writes it to the target IO



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/yle_tf/system/io_handlers.rb', line 107

def self.copy_data(source, target, **opts)
  while (data = source.readpartial(BLOCK_SIZE))
    target.write(data)
  end
rescue EOFError # rubocop:disable Lint/SuppressedException
  # All read
rescue IOError => e
  YleTf::Logger.debug e.full_message
ensure
  target.close_write if opts[:close_target]
end

.dev_null_inputObject

Returns a lambda that does nothing



70
71
72
# File 'lib/yle_tf/system/io_handlers.rb', line 70

def self.dev_null_input
  ->(*) {}
end

.dev_null_outputObject

Returns a lambda that just consumes the IO’s output



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/yle_tf/system/io_handlers.rb', line 75

def self.dev_null_output
  lambda do |io, *|
    Thread.new do
      begin
        while io.read; end
      rescue IOError => e
        YleTf::Logger.debug e.full_message
      end
    end
  end
end

.input_handler(handler) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yle_tf/system/io_handlers.rb', line 13

def self.input_handler(handler)
  case handler
  when :close
    close
  when :console
    console_input
  when :dev_null
    dev_null_input
  when IO, StringIO
    io_input(handler)
  else
    if !handler.respond_to?(:call)
      raise YleTf::Error, "Unknown input handler #{handler.inspect}"
    end

    handler
  end
end

.io_input(source) ⇒ Object

Returns a lambda that pipes the source IO to the IO’s input



88
89
90
91
92
93
94
# File 'lib/yle_tf/system/io_handlers.rb', line 88

def self.io_input(source)
  lambda do |target, *|
    Thread.new do
      copy_data(source, target, close_target: true)
    end
  end
end

.io_output(target) ⇒ Object

Returns a lambda that pipes IO’s output to the target IO Does not close the target stream



98
99
100
101
102
103
104
# File 'lib/yle_tf/system/io_handlers.rb', line 98

def self.io_output(target)
  lambda do |source, *|
    Thread.new do
      copy_data(source, target)
    end
  end
end

.output_handler(handler) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yle_tf/system/io_handlers.rb', line 32

def self.output_handler(handler)
  case handler
  when :close
    close
  when :console
    console_output
  when :dev_null
    dev_null_output
  when IO, StringIO
    io_output(handler)
  when Symbol
    OutputLogger.new(handler)
  else
    if !handler.respond_to?(:call)
      raise YleTf::Error, "Unknown output handler #{handler.inspect}"
    end

    handler
  end
end