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
30 31 32 |
# File 'lib/cabriolet/system/io_system.rb', line 30 def close(handle) handle.close end |
#copy(src, dest, bytes) ⇒ void
This method returns an undefined value.
Copy bytes from source to destination
76 77 78 |
# File 'lib/cabriolet/system/io_system.rb', line 76 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)
85 86 87 |
# File 'lib/cabriolet/system/io_system.rb', line 85 def (_handle, ) warn "[Cabriolet] #{message}" if Cabriolet.verbose end |
#open(filename, mode) ⇒ FileHandle
Open a file for reading, writing, or updating
22 23 24 |
# File 'lib/cabriolet/system/io_system.rb', line 22 def open(filename, mode) FileHandle.new(filename, mode) end |
#read(handle, bytes) ⇒ String
Read bytes from a handle
39 40 41 |
# File 'lib/cabriolet/system/io_system.rb', line 39 def read(handle, bytes) handle.read(bytes) end |
#seek(handle, offset, whence) ⇒ Integer
Seek to a position in a handle
58 59 60 |
# File 'lib/cabriolet/system/io_system.rb', line 58 def seek(handle, offset, whence) handle.seek(offset, whence) end |
#tell(handle) ⇒ Integer
Get current position in a handle
66 67 68 |
# File 'lib/cabriolet/system/io_system.rb', line 66 def tell(handle) handle.tell end |
#write(handle, data) ⇒ Integer
Write bytes to a handle
48 49 50 |
# File 'lib/cabriolet/system/io_system.rb', line 48 def write(handle, data) handle.write(data) end |