Class: FormatParser::IOConstraint

Inherits:
Object
  • Object
show all
Defined in:
lib/io_constraint.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ IOConstraint

Returns a new instance of IOConstraint.



18
19
20
# File 'lib/io_constraint.rb', line 18

def initialize(io)
  @io = io
end

Instance Method Details

#posObject

Returns the current position/offset within the IO

Returns:

  • Integer



49
50
51
# File 'lib/io_constraint.rb', line 49

def pos
  @io.pos
end

#read(n_bytes) ⇒ String?

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

Parameters:

  • n_bytes (Integer)

Returns:

  • (String, nil)

    the content read from the IO or ‘nil` if no data was available



27
28
29
# File 'lib/io_constraint.rb', line 27

def read(n_bytes)
  @io.read(n_bytes)
end

#seek(to) ⇒ Object

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

Parameters:

  • to (Integer)

    offset in the IO

Returns:

  • Integer



35
36
37
# File 'lib/io_constraint.rb', line 35

def seek(to)
  @io.seek(to)
end

#sizeObject

Returns the size of the resource contained in the IO

Returns:

  • Integer



42
43
44
# File 'lib/io_constraint.rb', line 42

def size
  @io.size
end