Class: Torque::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/torque/file_system.rb

Overview

Creates a (limited) interface through which Torque can interact with the local file system

Supports:

  • File creation, reading, line-by-line iteration, and overwriting

  • Directory creation

  • Pathname checking

Instance Method Summary collapse

Constructor Details

#initializeFileSystem

Returns a new instance of FileSystem.



17
18
19
# File 'lib/torque/file_system.rb', line 17

def initialize
  # Do nothing; the file system's properties are by definition global
end

Instance Method Details

#file_create(filename, over = false) ⇒ Object

Creates a file, optionally overwriting an existing file to do so



23
24
25
26
27
28
29
# File 'lib/torque/file_system.rb', line 23

def file_create(filename, over=false)
  if !path_exist? filename || over
    File.open(filename, "w")
  else
    raise IOError.new "File #{filename} already exists"
  end
end

#file_each_line(filename) ⇒ Object

Returns An iterator over each line of a file.

Returns:

  • An iterator over each line of a file



33
34
35
# File 'lib/torque/file_system.rb', line 33

def file_each_line(filename)
  File.open(filename, "r").each_line
end

#file_read(filename) ⇒ Object

Returns The contents of a file.

Returns:

  • The contents of a file



39
40
41
# File 'lib/torque/file_system.rb', line 39

def file_read(filename)
  File.read(filename)
end

#file_write(filename, string) ⇒ Object

Writes a string to file



45
46
47
# File 'lib/torque/file_system.rb', line 45

def file_write(filename, string)
  File.write(filename, string)
end

#mkdir(dirname) ⇒ Object

Creates a directory



51
52
53
# File 'lib/torque/file_system.rb', line 51

def mkdir(dirname)
  FileUtils.mkdir(dirname)
end

#mkdir_p(dirname) ⇒ Object

Creates a directory, creating intermediate directories as needed



57
58
59
# File 'lib/torque/file_system.rb', line 57

def mkdir_p(dirname)
  FileUtils.mkdir_p(dirname)
end

#path_exist?(pathname) ⇒ Boolean

Returns True if the pathname exists, else false.

Parameters:

  • pathname

    The pathname to test

Returns:

  • (Boolean)

    True if the pathname exists, else false



65
66
67
# File 'lib/torque/file_system.rb', line 65

def path_exist?(pathname)
  Pathname.new(pathname).exist?
end