class YleTf::System::IOHandlers

Constants

BLOCK_SIZE

Public Class Methods

close() click to toggle source

Returns a lambda that just closes the IO

# File lib/yle_tf/system/io_handlers.rb, line 55
def self.close
  ->(io, *) { io.close }
end
console_input() click to toggle source

Returns a lambda that pipes STDIN to the IO

# File lib/yle_tf/system/io_handlers.rb, line 60
def self.console_input
  io_input(STDIN)
end
console_output() click to toggle source

Returns a lambda that pipes IO's output to STDOUT

# File lib/yle_tf/system/io_handlers.rb, line 65
def self.console_output
  io_output(STDOUT)
end
copy_data(source, target, **opts) click to toggle source

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

# 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_input() click to toggle source

Returns a lambda that does nothing

# File lib/yle_tf/system/io_handlers.rb, line 70
def self.dev_null_input
  ->(*) {}
end
dev_null_output() click to toggle source

Returns a lambda that just consumes the IO's output

# 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) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source
# 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