Class: Rush::File
Overview
Files are a subclass of Rush::Entry. Most of the file-specific operations relate to manipulating the file’s contents, like search and replace.
Instance Attribute Summary
Attributes inherited from Entry
Instance Method Summary collapse
-
#append(contents) ⇒ Object
(also: #<<)
Append new contents to the end of the file, keeping what was in it.
-
#contents ⇒ Object
(also: #read, #cat)
Raw contents of the file.
-
#contents_or_blank ⇒ Object
Return the file’s contents, or if it doesn’t exist, a blank string.
-
#create ⇒ Object
(also: #touch)
Create a blank file.
- #dir? ⇒ Boolean
- #dirname ⇒ Object
- #entries ⇒ Object
-
#line_count ⇒ Object
Count the number of lines in the file.
-
#lines ⇒ Object
Return an array of lines from the file, similar to stdlib’s File#readlines.
-
#lines_or_empty ⇒ Object
Return an array of lines, or an empty array if the file does not exist.
-
#link(dst, options = {}) ⇒ Object
Hardlink the file (see File.ln for options).
-
#replace_contents!(pattern, replace_with) ⇒ Object
(also: #gsub, #s)
Search-and-replace file contents.
-
#search(pattern) ⇒ Object
Search the file’s for a regular expression.
-
#size ⇒ Object
Size in bytes on disk.
-
#write(new_contents) ⇒ Object
Write to the file, overwriting whatever was already in it.
Methods included from Commands
#edit, #open, #open_command, #open_with, #opt_to_s, #output_of
Methods inherited from Entry
#==, #access, #access=, #changed_at, #chown, #chown_R, #connection, #copy_to, #destroy, #duplicate, #executables, #exists?, factory, #full_path, #hidden?, #initialize, #inspect, #last_accessed, #last_modified, #method_missing, #mimic, #move_to, #owner, #owner=, #parent, #quoted_path, #rename, #symlink, #symlink?, #to_s
Constructor Details
This class inherits a constructor from Rush::Entry
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rush::Entry
Instance Method Details
#append(contents) ⇒ Object Also known as: <<
Append new contents to the end of the file, keeping what was in it.
47 48 49 |
# File 'lib/rush/file.rb', line 47 def append(contents) connection.append_to_file(full_path, contents) end |
#contents ⇒ Object Also known as: read, cat
Raw contents of the file. For non-text files, you probably want to avoid printing this on the screen.
32 33 34 |
# File 'lib/rush/file.rb', line 32 def contents connection.file_contents(full_path) end |
#contents_or_blank ⇒ Object
Return the file’s contents, or if it doesn’t exist, a blank string.
77 78 79 80 81 |
# File 'lib/rush/file.rb', line 77 def contents_or_blank contents rescue Rush::DoesNotExist "" end |
#create ⇒ Object Also known as: touch
Create a blank file.
13 14 15 16 |
# File 'lib/rush/file.rb', line 13 def create write('') self end |
#dir? ⇒ Boolean
4 5 6 |
# File 'lib/rush/file.rb', line 4 def dir? false end |
#dirname ⇒ Object
8 9 10 |
# File 'lib/rush/file.rb', line 8 def dirname ::File.dirname full_path end |
#entries ⇒ Object
97 98 99 |
# File 'lib/rush/file.rb', line 97 def entries [ self ] end |
#line_count ⇒ Object
Count the number of lines in the file.
84 85 86 |
# File 'lib/rush/file.rb', line 84 def line_count lines.size end |
#lines ⇒ Object
Return an array of lines from the file, similar to stdlib’s File#readlines.
53 54 55 |
# File 'lib/rush/file.rb', line 53 def lines contents.split("\n") end |
#lines_or_empty ⇒ Object
Return an array of lines, or an empty array if the file does not exist.
89 90 91 92 93 |
# File 'lib/rush/file.rb', line 89 def lines_or_empty lines rescue Rush::DoesNotExist [] end |
#link(dst, options = {}) ⇒ Object
Hardlink the file (see File.ln for options)
20 21 22 23 |
# File 'lib/rush/file.rb', line 20 def link(dst, = {}) connection.ln(full_path, dst, ) self.class.new(dst, box) end |
#replace_contents!(pattern, replace_with) ⇒ Object Also known as: gsub, s
Search-and-replace file contents.
Example: box.replace_contents!(/localhost/, ‘local.host’)
69 70 71 |
# File 'lib/rush/file.rb', line 69 def replace_contents!(pattern, replace_with) write contents.gsub(pattern, replace_with) end |
#search(pattern) ⇒ Object
Search the file’s for a regular expression. Returns nil if no match, or each of the matching lines in its entirety.
Example: box.search(/localhost/) # -> [ “127.0.0.1 localhostn”, “::1 localhostn” ]
61 62 63 64 |
# File 'lib/rush/file.rb', line 61 def search(pattern) matching_lines = lines.select { |line| line.match(pattern) } matching_lines.size == 0 ? nil : matching_lines end |
#size ⇒ Object
Size in bytes on disk.
26 27 28 |
# File 'lib/rush/file.rb', line 26 def size stat[:size] end |
#write(new_contents) ⇒ Object
Write to the file, overwriting whatever was already in it.
Example: file.write “hello, worldn”
42 43 44 |
# File 'lib/rush/file.rb', line 42 def write(new_contents) connection.write_file(full_path, new_contents) end |