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:



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

Parameters:

  • src (String)

    Source bytes

  • dest (String)

    Destination buffer

  • bytes (Integer)

    Number of bytes to copy



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)

Parameters:

  • handle (FileHandle, MemoryHandle, nil)

    Handle associated with message

  • message (String)

    Message to output



82
83
84
# File 'lib/cabriolet/system/io_system.rb', line 82

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



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

Parameters:

Returns:

  • (String)

    Bytes read (may be fewer than requested at EOF)



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

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



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

Parameters:

Returns:

  • (Integer)

    Current position



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

Parameters:

Returns:

  • (Integer)

    Number of bytes written



45
46
47
# File 'lib/cabriolet/system/io_system.rb', line 45

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