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, #replace_contents!, #search

Methods inherited from Entry

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

#[](key) ⇒ Object

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 Also known as: sh

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



122
123
124
# File 'lib/rush/dir.rb', line 122

def bash(command, options={})
	box.bash "cd #{full_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.



97
98
99
100
# File 'lib/rush/dir.rb', line 97

def create
	connection.create_dir(full_path)
	self
end

#create_dir(name) ⇒ Object

Create an empty subdir within this dir.



91
92
93
94
# File 'lib/rush/dir.rb', line 91

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

#create_file(name) ⇒ Object

Create a blank file within this dir.



84
85
86
87
88
# File 'lib/rush/dir.rb', line 84

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.



71
72
73
# File 'lib/rush/dir.rb', line 71

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

#entriesObject



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

def entries
	contents
end

#entries_treeObject

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



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

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.



66
67
68
# File 'lib/rush/dir.rb', line 66

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

#find_by_glob(glob) ⇒ Object

:nodoc:



54
55
56
57
58
# File 'lib/rush/dir.rb', line 54

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:



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

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

#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.



77
78
79
80
81
# File 'lib/rush/dir.rb', line 77

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

#nonhidden_dirsObject

Contained dirs that are not hidden.



108
109
110
111
112
# File 'lib/rush/dir.rb', line 108

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

#nonhidden_filesObject

Contained files that are not hidden.



115
116
117
118
119
# File 'lib/rush/dir.rb', line 115

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

#sizeObject

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



103
104
105
# File 'lib/rush/dir.rb', line 103

def size
	connection.size(full_path)
end