Class: Cabriolet::System::FileHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/system/file_handle.rb

Overview

FileHandle provides file I/O operations using the Ruby File class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, mode) ⇒ FileHandle

Initialize a new file handle

Parameters:

  • filename (String)

    Path to the file

  • mode (Integer)

    One of MODE_READ, MODE_WRITE, MODE_UPDATE, MODE_APPEND

Raises:

  • (IOError)

    if the file cannot be opened



14
15
16
17
18
# File 'lib/cabriolet/system/file_handle.rb', line 14

def initialize(filename, mode)
  @filename = filename
  @mode = mode
  @file = open_file(mode)
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/cabriolet/system/file_handle.rb', line 7

def filename
  @filename
end

#modeObject (readonly)

Returns the value of attribute mode.



7
8
9
# File 'lib/cabriolet/system/file_handle.rb', line 7

def mode
  @mode
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close the file



77
78
79
80
# File 'lib/cabriolet/system/file_handle.rb', line 77

def close
  @file.flush unless @file.closed?
  @file.close unless @file.closed?
end

#closed?Boolean

Check if the file is closed

Returns:

  • (Boolean)


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

def closed?
  @file.closed?
end

#flushvoid

This method returns an undefined value.

Flush the file buffer



70
71
72
# File 'lib/cabriolet/system/file_handle.rb', line 70

def flush
  @file.flush unless @file.closed?
end

#read(bytes) ⇒ String

Read bytes from the file

Parameters:

  • bytes (Integer)

    Number of bytes to read

Returns:

  • (String)

    Bytes read (binary encoding)



24
25
26
# File 'lib/cabriolet/system/file_handle.rb', line 24

def read(bytes)
  @file.read(bytes) || ""
end

#seek(offset, whence) ⇒ Integer

Seek to a position in the file

Parameters:

  • offset (Integer)

    Offset to seek to

  • whence (Integer)

    One of SEEK_START, SEEK_CUR, SEEK_END

Returns:

  • (Integer)

    New position



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cabriolet/system/file_handle.rb', line 41

def seek(offset, whence)
  io_whence = case whence
              when Constants::SEEK_START then ::IO::SEEK_SET
              when Constants::SEEK_CUR then ::IO::SEEK_CUR
              when Constants::SEEK_END then ::IO::SEEK_END
              else
                raise ArgumentError, "Invalid whence value: #{whence}"
              end
  @file.seek(offset, io_whence)
  @file.pos
end

#sizeInteger

Get the size of the file

Returns:

  • (Integer)

    File size in bytes



63
64
65
# File 'lib/cabriolet/system/file_handle.rb', line 63

def size
  @file.size
end

#tellInteger

Get current position in the file

Returns:

  • (Integer)

    Current position



56
57
58
# File 'lib/cabriolet/system/file_handle.rb', line 56

def tell
  @file.pos
end

#write(data) ⇒ Integer

Write bytes to the file

Parameters:

  • data (String)

    Data to write

Returns:

  • (Integer)

    Number of bytes written



32
33
34
# File 'lib/cabriolet/system/file_handle.rb', line 32

def write(data)
  @file.write(data)
end