Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/riel/file.rb
Class Method Summary collapse
-
._to_pathname(file) ⇒ Object
Converts the argument to a Pathname.
- .binary?(file) ⇒ Boolean
-
.copy_files(dir, files) ⇒ Object
Copies the files to the given directory, creating it if it does not exist.
-
.find_directories(dir) ⇒ Object
Returns an array of all directory under the given directory.
-
.find_files(dir) ⇒ Object
Returns an array of all files under the given directory.
-
.get_unused_file_name(basename) ⇒ Object
Returns a file for the given basename, sequentially appending an integer until one is found that does not exist.
-
.is_directory?(fd) ⇒ Boolean
Returns whether the given object is a directory.
-
.is_file?(fd) ⇒ Boolean
Returns whether the given object is a file.
-
.is_writable?(file) ⇒ Boolean
Returns whether the given file is writable.
-
.mkdir(dir) ⇒ Object
Creates the given directory.
-
.move_files(dir, files) ⇒ Object
Moves the files to the given directory, creating it if it does not exist.
-
.open_via_temp_file(file, tempfile = nil, tempdir = Dir::tmpdir, &blk) ⇒ Object
Opens a tempfile for writing, delegates to the given block, and renames the temp file to
file. -
.open_writable_file(file, &blk) ⇒ Object
Opens a file for writing and delegates to the given block.
-
.put_file(file, &blk) ⇒ Object
Writes a file, using puts (thus making it better for text files).
-
.put_via_temp_file(file, &blk) ⇒ Object
Writes a file, using puts, buffering it via a temp file.
-
.read_file(file, &blk) ⇒ Object
Reads a file line by line.
-
.read_file_lines(file, &blk) ⇒ Object
Reads a file line by line, calling the given block.
-
.remove_recursively(fd) ⇒ Object
Removes the file/directory, including all subelements.
- .text?(file) ⇒ Boolean
-
.write_file(file, &blk) ⇒ Object
Writes a file, using the
writemethod. -
.write_via_temp_file(file, &blk) ⇒ Object
Writes a file, using write, buffering it via a temp file.
Class Method Details
._to_pathname(file) ⇒ Object
Converts the argument to a Pathname.
104 105 106 |
# File 'lib/riel/file.rb', line 104 def self._to_pathname file file.kind_of?(Pathname) ? file : Pathname.new(file.to_s) end |
.binary?(file) ⇒ Boolean
22 23 24 |
# File 'lib/riel/file.rb', line 22 def self.binary? file return !self.text?(file) end |
.copy_files(dir, files) ⇒ Object
Copies the files to the given directory, creating it if it does not exist.
95 96 97 98 99 100 101 |
# File 'lib/riel/file.rb', line 95 def self.copy_files dir, files mkdir dir files.each do |file| FileUtils.copy file.to_s, dir.to_s end end |
.find_directories(dir) ⇒ Object
Returns an array of all directory under the given directory.
64 65 66 67 68 |
# File 'lib/riel/file.rb', line 64 def self.find_directories dir dirs = Array.new Find.find(dir) { |d| dirs.push(d) if is_directory?(d) } dirs end |
.find_files(dir) ⇒ Object
Returns an array of all files under the given directory.
57 58 59 60 61 |
# File 'lib/riel/file.rb', line 57 def self.find_files dir files = Array.new Find.find(dir) { |f| files.push(f) if is_file?(f) } files end |
.get_unused_file_name(basename) ⇒ Object
Returns a file for the given basename, sequentially appending an integer until one is found that does not exist. For example, “foo.3” if “foo”, “foo.1”, and “foo.2” already exist.
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/riel/file.rb', line 198 def self.get_unused_file_name basename tgt = basename if tgt.exist? i = 1 while tgt.exist? tgt = Pathname.new(basename.to_s + "." + i.to_s) i += 1 end end tgt end |
.is_directory?(fd) ⇒ Boolean
Returns whether the given object is a directory. Ignores errors.
47 48 49 50 51 52 53 54 |
# File 'lib/riel/file.rb', line 47 def self.is_directory? fd begin return self.stat(fd).directory? rescue # ignore files that could not be read, etc. return false end end |
.is_file?(fd) ⇒ Boolean
Returns whether the given object is a file. Ignores errors.
27 28 29 30 31 32 33 34 |
# File 'lib/riel/file.rb', line 27 def self.is_file? fd begin return self.stat(fd).file? rescue # ignore files that could not be read, etc. return false end end |
.is_writable?(file) ⇒ Boolean
Returns whether the given file is writable. Ignores errors.
37 38 39 40 41 42 43 44 |
# File 'lib/riel/file.rb', line 37 def self.is_writable? file begin return self.stat(file).writable? rescue # ignore files that could not be read, etc. return false end end |
.mkdir(dir) ⇒ Object
Creates the given directory.
80 81 82 83 |
# File 'lib/riel/file.rb', line 80 def self.mkdir dir pn = Pathname.new dir pn.mkdir unless pn.exist? end |
.move_files(dir, files) ⇒ Object
Moves the files to the given directory, creating it if it does not exist.
86 87 88 89 90 91 92 |
# File 'lib/riel/file.rb', line 86 def self.move_files dir, files mkdir dir files.each do |file| FileUtils.move file.to_s, dir.to_s end end |
.open_via_temp_file(file, tempfile = nil, tempdir = Dir::tmpdir, &blk) ⇒ Object
Opens a tempfile for writing, delegates to the given block, and renames the temp file to file. If tempfile is specified, it will be used as the temp file name. Ditto for tempdir, which defaults to Dir::tmpdir (e.g. “/tmp”)
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/riel/file.rb', line 167 def self.open_via_temp_file(file, tempfile = nil, tempdir = Dir::tmpdir, &blk) tempname = nil fpn = _to_pathname file tempfile ||= fpn.rootname Tempfile.open(tempfile) do |tf| blk.call tf tempname = tf.path end FileUtils.mv tempname, file.to_s end |
.open_writable_file(file, &blk) ⇒ Object
Opens a file for writing and delegates to the given block.
139 140 141 142 143 144 145 146 |
# File 'lib/riel/file.rb', line 139 def self.open_writable_file(file, &blk) fpn = _to_pathname file fpn.open(File::WRONLY | File::TRUNC | File::CREAT) do |f| blk.call f end fpn end |
.put_file(file, &blk) ⇒ Object
Writes a file, using puts (thus making it better for text files).
156 157 158 159 160 |
# File 'lib/riel/file.rb', line 156 def self.put_file(file, &blk) open_writable_file(file) do |io| io.puts blk.call end end |
.put_via_temp_file(file, &blk) ⇒ Object
Writes a file, using puts, buffering it via a temp file.
189 190 191 192 193 |
# File 'lib/riel/file.rb', line 189 def self.put_via_temp_file(file, &blk) open_via_temp_file(file) do |io| io.puts blk.call end end |
.read_file(file, &blk) ⇒ Object
Reads a file line by line. Returns the pathname for the file, or nil if it does not exist.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/riel/file.rb', line 110 def self.read_file file, &blk fpn = _to_pathname file if fpn.exist? fpn.open do |f| blk.call f.read end fpn else nil end end |
.read_file_lines(file, &blk) ⇒ Object
Reads a file line by line, calling the given block. Returns the pathname for the file, or nil if it does not exist.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/riel/file.rb', line 124 def self.read_file_lines(file, &blk) fpn = _to_pathname file if fpn.exist? fpn.open do |f| f.each_line do |line| blk.call line end fpn end else nil end end |
.remove_recursively(fd) ⇒ Object
Removes the file/directory, including all subelements. Use with caution!
71 72 73 74 75 76 77 |
# File 'lib/riel/file.rb', line 71 def self.remove_recursively fd #$$$ this is rmtree if fd.directory? fd.children.each { |x| remove_recursively x } end fd.unlink end |
.text?(file) ⇒ Boolean
18 19 20 |
# File 'lib/riel/file.rb', line 18 def self.text? file return File.file?(file) && FileType.instance.text?(file) end |
.write_file(file, &blk) ⇒ Object
Writes a file, using the write method.
149 150 151 152 153 |
# File 'lib/riel/file.rb', line 149 def self.write_file(file, &blk) open_writable_file(file) do |io| io.write blk.call end end |
.write_via_temp_file(file, &blk) ⇒ Object
Writes a file, using write, buffering it via a temp file.
182 183 184 185 186 |
# File 'lib/riel/file.rb', line 182 def self.write_via_temp_file(file, &blk) open_via_temp_file(file) do |io| io.write blk.call end end |