Class: FormatParser::IOConstraint
- Inherits:
-
Object
- Object
- FormatParser::IOConstraint
- 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
-
#initialize(io) ⇒ IOConstraint
constructor
A new instance of IOConstraint.
-
#pos ⇒ Object
Returns the current position/offset within the IO.
-
#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.
-
#seek(to) ⇒ Object
Seeks the IO to the given absolute offset from the start of the file/resource.
-
#size ⇒ Object
Returns the size of the resource contained in the IO.
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
#pos ⇒ Object
Returns the current position/offset within the IO
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
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
35 36 37 |
# File 'lib/io_constraint.rb', line 35 def seek(to) @io.seek(to) end |
#size ⇒ Object
Returns the size of the resource contained in the IO
42 43 44 |
# File 'lib/io_constraint.rb', line 42 def size @io.size end |