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
-
#contents ⇒ Object
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
Create a blank file.
- #dir? ⇒ Boolean
- #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.
-
#replace_contents!(pattern, replace_with) ⇒ Object
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 inherited from Entry
#==, #access, #access=, #connection, #copy_to, #created_at, #destroy, #duplicate, #exists?, factory, #full_path, #hidden?, #initialize, #inspect, #last_accessed, #last_modified, #mimic, #move_to, #parent, #rename, #to_s
Constructor Details
This class inherits a constructor from Rush::Entry
Instance Method Details
#contents ⇒ Object
Raw contents of the file. For non-text files, you probably want to avoid printing this on the screen.
21 22 23 |
# File 'lib/rush/file.rb', line 21 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.
54 55 56 57 58 |
# File 'lib/rush/file.rb', line 54 def contents_or_blank contents rescue Rush::DoesNotExist "" end |
#create ⇒ Object
Create a blank file.
9 10 11 12 |
# File 'lib/rush/file.rb', line 9 def create write('') self end |
#dir? ⇒ Boolean
4 5 6 |
# File 'lib/rush/file.rb', line 4 def dir? false end |
#entries ⇒ Object
74 75 76 |
# File 'lib/rush/file.rb', line 74 def entries [ self ] end |
#line_count ⇒ Object
Count the number of lines in the file.
61 62 63 |
# File 'lib/rush/file.rb', line 61 def line_count lines.size end |
#lines ⇒ Object
Return an array of lines from the file, similar to stdlib’s File#readlines.
33 34 35 |
# File 'lib/rush/file.rb', line 33 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.
66 67 68 69 70 |
# File 'lib/rush/file.rb', line 66 def lines_or_empty lines rescue Rush::DoesNotExist [] end |
#replace_contents!(pattern, replace_with) ⇒ Object
Search-and-replace file contents.
Example: box.replace_contents!(/localhost/, ‘local.host’)
49 50 51 |
# File 'lib/rush/file.rb', line 49 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” ]
41 42 43 44 |
# File 'lib/rush/file.rb', line 41 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.
15 16 17 |
# File 'lib/rush/file.rb', line 15 def size stat[:size] end |
#write(new_contents) ⇒ Object
Write to the file, overwriting whatever was already in it.
Example: file.write “hello, worldn”
28 29 30 |
# File 'lib/rush/file.rb', line 28 def write(new_contents) connection.write_file(full_path, new_contents) end |