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 included from Commands

#mate, #vi

Methods inherited from Entry

#==, #access, #access=, #changed_at, #connection, #copy_to, #destroy, #duplicate, #exists?, factory, #full_path, #hidden?, #initialize, #inspect, #last_accessed, #last_modified, #mimic, #move_to, #parent, #quoted_path, #rename, #to_s

Constructor Details

This class inherits a constructor from 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.



35
36
37
# File 'lib/rush/file.rb', line 35

def append(contents)
	connection.append_to_file(full_path, contents)
end

#contentsObject Also known as: read

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.



62
63
64
65
66
# File 'lib/rush/file.rb', line 62

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



82
83
84
# File 'lib/rush/file.rb', line 82

def entries
	[ self ]
end

#line_countObject

Count the number of lines in the file.



69
70
71
# File 'lib/rush/file.rb', line 69

def line_count
	lines.size
end

#linesObject

Return an array of lines from the file, similar to stdlib’s File#readlines.



41
42
43
# File 'lib/rush/file.rb', line 41

def lines
	contents.split("\n")
end

#lines_or_emptyObject

Return an array of lines, or an empty array if the file does not exist.



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

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’)



57
58
59
# File 'lib/rush/file.rb', line 57

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” ]



49
50
51
52
# File 'lib/rush/file.rb', line 49

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”



30
31
32
# File 'lib/rush/file.rb', line 30

def write(new_contents)
	connection.write_file(full_path, new_contents)
end