Class: Cabriolet::System::IOSystem
- Inherits:
-
Object
- Object
- Cabriolet::System::IOSystem
- Defined in:
- lib/cabriolet/system/io_system.rb
Overview
IOSystem provides an abstraction layer for file I/O operations, enabling dependency injection and custom I/O implementations.
This allows for:
-
Testing with mock I/O
-
In-memory operations
-
Custom I/O sources (network, etc.)
Instance Method Summary collapse
-
#close(handle) ⇒ void
Close a file handle.
-
#copy(src, dest, bytes) ⇒ void
Copy bytes from source to destination.
-
#message(_handle, message) ⇒ void
Output a message (for debugging/logging).
-
#open(filename, mode) ⇒ FileHandle
Open a file for reading, writing, or updating.
-
#read(handle, bytes) ⇒ String
Read bytes from a handle.
-
#seek(handle, offset, whence) ⇒ Integer
Seek to a position in a handle.
-
#tell(handle) ⇒ Integer
Get current position in a handle.
-
#write(handle, data) ⇒ Integer
Write bytes to a handle.
Instance Method Details
#close(handle) ⇒ void
This method returns an undefined value.
Close a file handle
27 28 29 |
# File 'lib/cabriolet/system/io_system.rb', line 27 def close(handle) handle.close end |
#copy(src, dest, bytes) ⇒ void
This method returns an undefined value.
Copy bytes from source to destination
73 74 75 |
# File 'lib/cabriolet/system/io_system.rb', line 73 def copy(src, dest, bytes) dest.replace(src.byteslice(0, bytes)) end |
#message(_handle, message) ⇒ void
This method returns an undefined value.
Output a message (for debugging/logging)
82 83 84 |
# File 'lib/cabriolet/system/io_system.rb', line 82 def (_handle, ) warn "[Cabriolet] #{message}" if Cabriolet.verbose end |
#open(filename, mode) ⇒ FileHandle
Open a file for reading, writing, or updating
19 20 21 |
# File 'lib/cabriolet/system/io_system.rb', line 19 def open(filename, mode) FileHandle.new(filename, mode) end |
#read(handle, bytes) ⇒ String
Read bytes from a handle
36 37 38 |
# File 'lib/cabriolet/system/io_system.rb', line 36 def read(handle, bytes) handle.read(bytes) end |
#seek(handle, offset, whence) ⇒ Integer
Seek to a position in a handle
55 56 57 |
# File 'lib/cabriolet/system/io_system.rb', line 55 def seek(handle, offset, whence) handle.seek(offset, whence) end |
#tell(handle) ⇒ Integer
Get current position in a handle
63 64 65 |
# File 'lib/cabriolet/system/io_system.rb', line 63 def tell(handle) handle.tell end |
#write(handle, data) ⇒ Integer
Write bytes to a handle
45 46 47 |
# File 'lib/cabriolet/system/io_system.rb', line 45 def write(handle, data) handle.write(data) end |