Class: Torque::FileSystem
- Inherits:
-
Object
- Object
- Torque::FileSystem
- 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
-
#file_create(filename, over = false) ⇒ Object
Creates a file, optionally overwriting an existing file to do so.
-
#file_each_line(filename) ⇒ Object
Returns an iterator over each line of a file.
-
#file_read(filename) ⇒ Object
Returns the contents of a file.
-
#file_write(filename, string) ⇒ Object
Writes a string to file.
-
#initialize ⇒ FileSystem
constructor
A new instance of FileSystem.
-
#mkdir(dirname) ⇒ Object
Creates a directory.
-
#mkdir_p(dirname) ⇒ Object
Creates a directory, creating intermediate directories as needed.
-
#path_exist?(pathname) ⇒ Boolean
Returns true if a pathname exists, else false.
Constructor Details
#initialize ⇒ FileSystem
Returns a new instance of FileSystem.
19 20 21 |
# File 'lib/torque/file_system.rb', line 19 def initialize # Do nothing. The file system's properties are automatically global end |
Instance Method Details
#file_create(filename, over = false) ⇒ Object
Creates a file, optionally overwriting an existing file to do so
25 26 27 28 29 30 31 |
# File 'lib/torque/file_system.rb', line 25 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
35 36 37 |
# File 'lib/torque/file_system.rb', line 35 def file_each_line(filename) File.open(filename, "r").each_line end |
#file_read(filename) ⇒ Object
Returns the contents of a file
41 42 43 |
# File 'lib/torque/file_system.rb', line 41 def file_read(filename) File.read(filename) end |
#file_write(filename, string) ⇒ Object
Writes a string to file
47 48 49 |
# File 'lib/torque/file_system.rb', line 47 def file_write(filename, string) File.write(filename, string) end |
#mkdir(dirname) ⇒ Object
Creates a directory
53 54 55 |
# File 'lib/torque/file_system.rb', line 53 def mkdir(dirname) FileUtils.mkdir(dirname) end |
#mkdir_p(dirname) ⇒ Object
Creates a directory, creating intermediate directories as needed
59 60 61 |
# File 'lib/torque/file_system.rb', line 59 def mkdir_p(dirname) FileUtils.mkdir_p(dirname) end |
#path_exist?(pathname) ⇒ Boolean
Returns true if a pathname exists, else false
65 66 67 |
# File 'lib/torque/file_system.rb', line 65 def path_exist?(pathname) Pathname.new(pathname).exist? end |