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

/(key)

Slashes work as well, e.g. dir/'subdir/file'

Alias for: []
[](key) click to toggle source

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
Also aliased as: /
bash(command, options={}) click to toggle source

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
contents() click to toggle source

Entries contained within this dir - not recursive.

# File lib/rush/dir.rb, line 25
def contents
        find_by_glob('*') + find_by_glob('.[!.]*')
end
create() click to toggle source

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_dir(name) click to toggle source

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_file(name) click to toggle source

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
dir?() click to toggle source
# File lib/rush/dir.rb, line 15
def dir?
        true
end
dirname()
Alias for: full_path
dirs() click to toggle source

Other dirs contained in this dir only.

# File lib/rush/dir.rb, line 35
def dirs
        contents.select(&:dir?)
end
dirs_flattened() click to toggle source

Recursively contained dirs.

# File lib/rush/dir.rb, line 84
def dirs_flattened
        entries_tree.select(&:dir?)
end
entries() click to toggle source
# File lib/rush/dir.rb, line 156
def entries
        contents
end
entries_tree() click to toggle source

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() click to toggle source

Files contained in this dir only.

# File lib/rush/dir.rb, line 30
def files
        contents.reject(&:dir?)
end
files_flattened() click to toggle source

Recursively contained files.

# File lib/rush/dir.rb, line 79
def files_flattened
        entries_tree.reject(&:dir?)
end
full_path() click to toggle source
# File lib/rush/dir.rb, line 19
def full_path
        "#{super}/"
end
Also aliased as: dirname
git(*args) click to toggle source

Run git within this dir.

# File lib/rush/dir.rb, line 150
def git(*args)
        bash "git #{args.join(' ')}"
end
locate(path) click to toggle source
# 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
locate_dir(path) click to toggle source
# 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
ls(*args) click to toggle source

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
make_entries(filenames) click to toggle source

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
nonhidden_dirs() click to toggle source

Contained dirs that are not hidden.

# File lib/rush/dir.rb, line 120
def nonhidden_dirs
        dirs.reject(&:hidden?)
end
nonhidden_files() click to toggle source

Contained files that are not hidden.

# File lib/rush/dir.rb, line 125
def nonhidden_files
        files.reject(&:hidden?)
end
purge() click to toggle source

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
rake(*args) click to toggle source

Run rake within this dir.

# File lib/rush/dir.rb, line 145
def rake(*args)
        bash "rake #{args.join(' ')}"
end
size() click to toggle source

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