class FormatParser::IOConstraint

We deliberately want to document and restrict the number of methods an IO-ish object has to implement to be usable with all our parsers. This subset is fairly thin and well defined, and all the various IO limiters and cache facilities in the library are guaranteed to support those methods. This wrapper is used to guarantee that the parser can only call those specific methods and nothing more. Consequently, if the parser uses a gem that for some reason needs additional IO methods to be available this parser has to provide it's own extensions to that end.

The rationale for including a method in this subset is as follows: we include a method if other methods can be implemented on top of it. For example, should some parser desire `IO#readbyte`, it can be implemented in terms of a `read()`. Idem for things like `IO#eof?`, `IO#rewind` and friends.

Public Class Methods

new(io) click to toggle source
# File lib/io_constraint.rb, line 18
def initialize(io)
  @io = io
end

Public Instance Methods

pos() click to toggle source

Returns the current position/offset within the IO

@return Integer

# File lib/io_constraint.rb, line 49
def pos
  @io.pos
end
read(n_bytes) click to toggle source

Returns at most `n_bytes` of data from the IO or less if less data was available before the EOF was hit

@param n_bytes @return [String, nil] the content read from the IO or `nil` if no data was available

# File lib/io_constraint.rb, line 27
def read(n_bytes)
  @io.read(n_bytes)
end
seek(to) click to toggle source

Seeks the IO to the given absolute offset from the start of the file/resource

@param to offset in the IO @return Integer

# File lib/io_constraint.rb, line 35
def seek(to)
  @io.seek(to)
end
size() click to toggle source

Returns the size of the resource contained in the IO

@return Integer

# File lib/io_constraint.rb, line 42
def size
  @io.size
end