Class: Rush::File

Inherits:
Entry
  • Object
show all
Includes:
Commands
Defined in:
lib/rush/file.rb

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

#box, #name, #path

Instance Method Summary collapse

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

#contentsObject

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_blankObject

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

#createObject

Create a blank file.



9
10
11
12
# File 'lib/rush/file.rb', line 9

def create
	write('')
	self
end

#dir?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/rush/file.rb', line 4

def dir?
	false
end

#entriesObject



74
75
76
# File 'lib/rush/file.rb', line 74

def entries
	[ self ]
end

#line_countObject

Count the number of lines in the file.



61
62
63
# File 'lib/rush/file.rb', line 61

def line_count
	lines.size
end

#linesObject

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_emptyObject

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

#sizeObject

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