Class: Cabriolet::System::IOSystem

Inherits:
Object
  • Object
show all
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

Instance Method Details

#close(handle) ⇒ void

This method returns an undefined value.

Close a file handle

Parameters:



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

Parameters:

  • src (String)

    Source bytes

  • dest (String)

    Destination buffer

  • bytes (Integer)

    Number of bytes to copy



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)

Parameters:

  • handle (FileHandle, MemoryHandle, nil)

    Handle associated with message

  • message (String)

    Message to output



85
86
87
# File 'lib/cabriolet/system/io_system.rb', line 85

def message(_handle, message)
  warn "[Cabriolet] #{message}" if Cabriolet.verbose
end

#open(filename, mode) ⇒ FileHandle

Open a file for reading, writing, or updating

Parameters:

  • filename (String)

    Path to the file

  • mode (Integer)

    One of MODE_READ, MODE_WRITE, MODE_UPDATE, MODE_APPEND

Returns:

  • (FileHandle)

    Handle for performing I/O operations

Raises:

  • (IOError)

    if the file cannot be opened



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

Parameters:

Returns:

  • (String)

    Bytes read (may be fewer than requested at EOF)



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

Parameters:

  • handle (FileHandle, MemoryHandle)

    Handle to seek in

  • offset (Integer)

    Offset to seek to

  • whence (Integer)

    One of SEEK_START, SEEK_CUR, SEEK_END

Returns:

  • (Integer)

    New position



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

Parameters:

Returns:

  • (Integer)

    Current position



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

Parameters:

Returns:

  • (Integer)

    Number of bytes written



48
49
50
# File 'lib/cabriolet/system/io_system.rb', line 48

def write(handle, data)
  handle.write(data)
end