class MarshalStream

Private: Wrapper around an IO object to read/write Marshaled objects.

Constants

DEFAULT_MAX_OUTBOX

Attributes

inbox[R]
io[R]
max_outbox[R]
outbox[R]

Public Class Methods

new(io, max_outbox: DEFAULT_MAX_OUTBOX) click to toggle source
# File lib/aggro/marshal_stream.rb, line 12
def initialize(io, max_outbox: DEFAULT_MAX_OUTBOX)
  @io = io
  @max_outbox = max_outbox
  @inbox = []
  @outbox = []
end

Public Instance Methods

<<(*objects)
Alias for: write
close() click to toggle source
# File lib/aggro/marshal_stream.rb, line 19
def close
  flush_outbox
  io.close
end
closed?() click to toggle source
# File lib/aggro/marshal_stream.rb, line 24
def closed?
  io.closed?
end
each() { |obj| ... } click to toggle source
# File lib/aggro/marshal_stream.rb, line 28
def each
  return to_enum unless block_given?

  read { |obj| yield obj } until eof
end
eof()
Alias for: eof?
eof?() click to toggle source
# File lib/aggro/marshal_stream.rb, line 34
def eof?
  inbox.empty? && io.eof?
end
Also aliased as: eof
flush_buffer() click to toggle source
# File lib/aggro/marshal_stream.rb, line 40
def flush_buffer
  self
end
flush_outbox() click to toggle source
# File lib/aggro/marshal_stream.rb, line 44
def flush_outbox
  outbox.each { |obj| write_to_stream(obj.is_a? Proc ? obj.call : obj) }
  outbox.clear

  self
end
read() { |obj| ... } click to toggle source
# File lib/aggro/marshal_stream.rb, line 51
def read
  if block_given?
    read_from_inbox { |obj| yield obj }
    read_from_stream { |obj| yield obj }

    nil
  else
    read_one
  end
end
read_from_stream() { |load| ... } click to toggle source
# File lib/aggro/marshal_stream.rb, line 62
def read_from_stream
  yield Marshal.load(io)
rescue IOError, SystemCallError
  raise
rescue => e
  raise StreamError, "Unreadble stream: #{e}"
end
read_one() click to toggle source
# File lib/aggro/marshal_stream.rb, line 70
def read_one
  return inbox.shift unless inbox.empty?

  result = nil

  read { |obj| result.nil? ? result = obj : (inbox << obj) } while result.nil?

  result
end
to_io() click to toggle source
# File lib/aggro/marshal_stream.rb, line 80
def to_io
  io
end
write(*objects) click to toggle source
# File lib/aggro/marshal_stream.rb, line 84
def write(*objects)
  write_to_buffer(*objects)
  flush_buffer
end
Also aliased as: <<
write_to_buffer(*objects) click to toggle source
# File lib/aggro/marshal_stream.rb, line 91
def write_to_buffer(*objects)
  flush_outbox
  objects.each { |object| write_to_stream object }

  self
end
write_to_outbox(object = nil, &block) click to toggle source
# File lib/aggro/marshal_stream.rb, line 98
def write_to_outbox(object = nil, &block)
  outbox << (block || object)

  flush_outbox if outbox.size > max_outbox

  self
end
write_to_stream(object) click to toggle source
# File lib/aggro/marshal_stream.rb, line 106
def write_to_stream(object)
  Marshal.dump(object, io)

  self
end

Private Instance Methods

read_from_inbox() { |obj| ... } click to toggle source
# File lib/aggro/marshal_stream.rb, line 117
def read_from_inbox
  return if inbox.empty?

  inbox.each { |obj| yield obj }
  inbox.clear
end