Module: Rush::Commands

Included in:
Array, Dir, File, SearchResults
Defined in:
lib/rush/commands.rb

Overview

The commands module contains operations against Rush::File entries, and is mixed in to Rush::Entry and Array. This means you can run these commands against a single file, a dir full of files, or an arbitrary list of files.

Examples:

box['/etc/hosts'].search /localhost/       # single file
box['/etc/'].search /localhost/            # entire directory
box['/etc/**/*.conf'].search /localhost/   # arbitrary list

Instance Method Summary collapse

Instance Method Details

#entriesObject

The entries command must return an array of Rush::Entry items. This varies by class that it is mixed in to.



13
14
15
# File 'lib/rush/commands.rb', line 13

def entries
	raise "must define me in class mixed in to for command use"
end

#line_countObject

Count the number of lines in the contained files.



37
38
39
40
41
42
# File 'lib/rush/commands.rb', line 37

def line_count
	entries.inject(0) do |count, entry|
		count += entry.lines.size if !entry.dir?
		count
	end
end

#mate(*args) ⇒ Object

Invoke TextMate on one or more files - only works locally.



51
52
53
54
# File 'lib/rush/commands.rb', line 51

def mate(*args)
	names = entries.map { |f| f.quoted_path }.join(' ')
	system "mate #{names} #{args.join(' ')}"
end

#replace_contents!(pattern, with_text) ⇒ Object

Search and replace file contents.



30
31
32
33
34
# File 'lib/rush/commands.rb', line 30

def replace_contents!(pattern, with_text)
	entries.each do |entry|
		entry.replace_contents!(pattern, with_text) unless entry.dir?
	end
end

#search(pattern) ⇒ Object

Search file contents for a regular expression. A Rush::SearchResults object is returned.



19
20
21
22
23
24
25
26
27
# File 'lib/rush/commands.rb', line 19

def search(pattern)
	results = Rush::SearchResults.new(pattern)
	entries.each do |entry|
		if !entry.dir? and matches = entry.search(pattern)
			results.add(entry, matches)
		end
	end
	results
end

#vi(*args) ⇒ Object

Invoke vi on one or more files - only works locally.



45
46
47
48
# File 'lib/rush/commands.rb', line 45

def vi(*args)
	names = entries.map { |f| f.quoted_path }.join(' ')
	system "vim #{names} #{args.join(' ')}"
end