Class: Rush::Dir

Inherits:
Entry show all
Includes:
Commands
Defined in:
lib/rush/dir.rb

Overview

A dir is a subclass of Rush::Entry that contains other entries. Also known as a directory or a folder.

Dirs can be operated on with Rush::Commands the same as an array of files. They also offer a square bracket accessor which can use globbing to get a list of files.

Example:

dir = box['/home/adam/']
dir['**/*.rb'].line_count

In the interactive shell, dir.ls is a useful command.

Instance Attribute Summary

Attributes inherited from Entry

#box, #name, #path

Instance Method Summary collapse

Methods included from Commands

#line_count, #mate, #replace_contents!, #search, #vi

Methods inherited from Entry

#==, #access, #access=, #changed_at, #connection, #copy_to, #destroy, #duplicate, #exists?, factory, #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

#[](key) ⇒ Object Also known as: /

Access subentries with square brackets, e.g. dir



39
40
41
42
43
44
45
46
47
48
# File 'lib/rush/dir.rb', line 39

def [](key)
	key = key.to_s
	if key == '**'
		files_flattened
	elsif key.match(/\*/)
		find_by_glob(key)
	else
		find_by_name(key)
	end
end

#bash(command, options = {}) ⇒ Object

Run a bash command starting in this directory. Options are the same as Rush::Box#bash.



124
125
126
# File 'lib/rush/dir.rb', line 124

def bash(command, options={})
	box.bash "cd #{quoted_path} && #{command}", options
end

#contentsObject

Entries contained within this dir - not recursive.



24
25
26
# File 'lib/rush/dir.rb', line 24

def contents
	find_by_glob('*')
end

#createObject

Create an instantiated but not yet filesystem-created dir.



99
100
101
102
# File 'lib/rush/dir.rb', line 99

def create
	connection.create_dir(full_path)
	self
end

#create_dir(name) ⇒ Object

Create an empty subdir within this dir.



93
94
95
96
# File 'lib/rush/dir.rb', line 93

def create_dir(name)
	name += '/' unless name.tail(1) == '/'
	self[name].create
end

#create_file(name) ⇒ Object

Create a blank file within this dir.



86
87
88
89
90
# File 'lib/rush/dir.rb', line 86

def create_file(name)
	file = self[name].create
	file.write('')
	file
end

#dir?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/rush/dir.rb', line 15

def dir?
	true
end

#dirsObject

Other dirs contained in this dir only.



34
35
36
# File 'lib/rush/dir.rb', line 34

def dirs
	contents.select { |entry| entry.dir? }
end

#dirs_flattenedObject

Recursively contained dirs.



73
74
75
# File 'lib/rush/dir.rb', line 73

def dirs_flattened
	entries_tree.select { |e| e.dir? }
end

#entriesObject



157
158
159
# File 'lib/rush/dir.rb', line 157

def entries
	contents
end

#entries_treeObject

A list of all the recursively contained entries in flat form.



63
64
65
# File 'lib/rush/dir.rb', line 63

def entries_tree
	find_by_glob('**/*')
end

#filesObject

Files contained in this dir only.



29
30
31
# File 'lib/rush/dir.rb', line 29

def files
	contents.select { |entry| !entry.dir? }
end

#files_flattenedObject

Recursively contained files.



68
69
70
# File 'lib/rush/dir.rb', line 68

def files_flattened
	entries_tree.select { |e| !e.dir? }
end

#find_by_glob(glob) ⇒ Object

:nodoc:



56
57
58
59
60
# File 'lib/rush/dir.rb', line 56

def find_by_glob(glob)    # :nodoc:
	connection.index(full_path, glob).map do |fname|
		Rush::Entry.factory("#{full_path}/#{fname}", box)
	end
end

#find_by_name(name) ⇒ Object

:nodoc:



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

def find_by_name(name)    # :nodoc:
	Rush::Entry.factory("#{full_path}/#{name}", box)
end

#full_pathObject



19
20
21
# File 'lib/rush/dir.rb', line 19

def full_path
	"#{super}/"
end

#git(*args) ⇒ Object

Run git within this dir.



151
152
153
# File 'lib/rush/dir.rb', line 151

def git(*args)
	bash "git #{args.join(' ')}"
end

#lsObject

Text output of dir listing, equivalent to the regular unix shell’s ls command.



134
135
136
137
138
139
140
141
142
143
# File 'lib/rush/dir.rb', line 134

def ls
	out = [ "#{self}" ]
	nonhidden_dirs.each do |dir|
		out << "  #{dir.name}/"
	end
	nonhidden_files.each do |file|
		out << "  #{file.name}"
	end
	out.join("\n")
end

#make_entries(filenames) ⇒ Object

Given a list of flat filenames, product a list of entries under this dir. Mostly for internal use.



79
80
81
82
83
# File 'lib/rush/dir.rb', line 79

def make_entries(filenames)
	filenames.map do |fname|
		Rush::Entry.factory("#{full_path}/#{fname}")
	end
end

#nonhidden_dirsObject

Contained dirs that are not hidden.



110
111
112
113
114
# File 'lib/rush/dir.rb', line 110

def nonhidden_dirs
	dirs.select do |dir|
		!dir.hidden?
	end
end

#nonhidden_filesObject

Contained files that are not hidden.



117
118
119
120
121
# File 'lib/rush/dir.rb', line 117

def nonhidden_files
	files.select do |file|
		!file.hidden?
	end
end

#purgeObject

Destroy all of the contents of the directory, leaving it fresh and clean.



129
130
131
# File 'lib/rush/dir.rb', line 129

def purge
	connection.purge full_path
end

#rake(*args) ⇒ Object

Run rake within this dir.



146
147
148
# File 'lib/rush/dir.rb', line 146

def rake(*args)
	bash "rake #{args.join(' ')}"
end

#sizeObject

Get the total disk usage of the dir and all its contents.



105
106
107
# File 'lib/rush/dir.rb', line 105

def size
	connection.size(full_path)
end