class Rush::Dir
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.
Public Instance Methods
Access subentries with square brackets, e.g. dir
# File lib/rush/dir.rb, line 40 def [](key) key = key.to_s case when key == '**' then files_flattened when key.match(/\*/) then find_by_glob(key) else find_by_name(key) end end
Run a bash command starting in this directory. Options are the same as Rush::Box#bash
.
# File lib/rush/dir.rb, line 130 def bash(command, options={}) box.bash "cd #{quoted_path} && #{command}", options end
Entries contained within this dir - not recursive.
# File lib/rush/dir.rb, line 25 def contents find_by_glob('*') + find_by_glob('.[!.]*') end
Create an instantiated but not yet filesystem-created dir.
# File lib/rush/dir.rb, line 109 def create connection.create_dir(full_path) self end
Create an empty subdir within this dir.
# File lib/rush/dir.rb, line 103 def create_dir(name) name += '/' unless name[-1] == '/' self[name].create end
Create a blank file within this dir.
# File lib/rush/dir.rb, line 96 def create_file(name) file = self[name].create file.write('') file end
# File lib/rush/dir.rb, line 15 def dir? true end
Other dirs contained in this dir only.
# File lib/rush/dir.rb, line 35 def dirs contents.select(&:dir?) end
Recursively contained dirs.
# File lib/rush/dir.rb, line 84 def dirs_flattened entries_tree.select(&:dir?) end
# File lib/rush/dir.rb, line 156 def entries contents end
A list of all the recursively contained entries in flat form.
# File lib/rush/dir.rb, line 74 def entries_tree find_by_glob('**/*') end
Files contained in this dir only.
# File lib/rush/dir.rb, line 30 def files contents.reject(&:dir?) end
Recursively contained files.
# File lib/rush/dir.rb, line 79 def files_flattened entries_tree.reject(&:dir?) end
# File lib/rush/dir.rb, line 19 def full_path "#{super}/" end
Run git within this dir.
# File lib/rush/dir.rb, line 150 def git(*args) bash "git #{args.join(' ')}" end
# File lib/rush/dir.rb, line 51 def locate(path) located = bash("locate -i #{path}").split("\n"). map { |x| x.dir? ? Rush::Dir.new(x) : Rush::File.new(x) } located.size == 1 ? located.first : located end
# File lib/rush/dir.rb, line 57 def locate_dir(path) located = bash("locate -r #{path}$").split("\n"). map { |x| Dir.new x } located.size == 1 ? located.first : located end
Text output of dir listing, equivalent to the regular unix shell's ls command.
# File lib/rush/dir.rb, line 140 def ls(*args) output_of 'ls', *args end
Given a list of flat filenames, product a list of entries under this dir. Mostly for internal use.
# File lib/rush/dir.rb, line 90 def make_entries(filenames) Array(filenames). map { |fname| Rush::Entry.factory("#{full_path}/#{fname}") } end
Destroy all of the contents of the directory, leaving it fresh and clean.
# File lib/rush/dir.rb, line 135 def purge connection.purge full_path end
Run rake within this dir.
# File lib/rush/dir.rb, line 145 def rake(*args) bash "rake #{args.join(' ')}" end
Get the total disk usage of the dir and all its contents.
# File lib/rush/dir.rb, line 115 def size connection.size(full_path) end