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



49
50
51
# File 'lib/yle_tf/system/io_handlers.rb', line 49

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

.console_inputObject

Returns a lambda that pipes STDIN to the IO



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

def self.console_input
  io_input(STDIN)
end

.console_outputObject

Returns a lambda that pipes IO’s output to STDOUT



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

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



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

def self.copy_data(source, target, **opts)
  while (data = source.readpartial(BLOCK_SIZE))
    target.write(data)
  end
rescue EOFError # rubocop:disable Lint/HandleExceptions
  # All read
ensure
  target.close_write if opts[:close_target]
end

.dev_null_inputObject

Returns a lambda that does nothing



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

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

.dev_null_outputObject

Returns a lambda that just consumes the IO’s output



69
70
71
72
73
74
75
# File 'lib/yle_tf/system/io_handlers.rb', line 69

def self.dev_null_output
  lambda do |io, *|
    Thread.new do
      while io.read; end
    end
  end
end

.input_handler(handler) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/yle_tf/system/io_handlers.rb', line 9

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



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

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



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

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

.output_handler(handler) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/yle_tf/system/io_handlers.rb', line 27

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