Class: Rush::Dir
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
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #/)
Access subentries with square brackets, e.g.
-
#bash(command, options = {}) ⇒ Object
Run a bash command starting in this directory.
-
#contents ⇒ Object
Entries contained within this dir - not recursive.
-
#create ⇒ Object
Create an instantiated but not yet filesystem-created dir.
-
#create_dir(name) ⇒ Object
Create an empty subdir within this dir.
-
#create_file(name) ⇒ Object
Create a blank file within this dir.
- #dir? ⇒ Boolean
-
#dirs ⇒ Object
Other dirs contained in this dir only.
-
#dirs_flattened ⇒ Object
Recursively contained dirs.
- #entries ⇒ Object
-
#entries_tree ⇒ Object
A list of all the recursively contained entries in flat form.
-
#files ⇒ Object
Files contained in this dir only.
-
#files_flattened ⇒ Object
Recursively contained files.
-
#find_by_glob(glob) ⇒ Object
:nodoc:.
-
#find_by_name(name) ⇒ Object
:nodoc:.
- #full_path ⇒ Object (also: #dirname)
-
#git(*args) ⇒ Object
Run git within this dir.
- #locate(path) ⇒ Object
- #locate_dir(path) ⇒ Object
-
#ls(*args) ⇒ Object
Text output of dir listing, equivalent to the regular unix shell’s ls command.
-
#make_entries(filenames) ⇒ Object
Given a list of flat filenames, product a list of entries under this dir.
-
#nonhidden_dirs ⇒ Object
Contained dirs that are not hidden.
-
#nonhidden_files ⇒ Object
Contained files that are not hidden.
-
#purge ⇒ Object
Destroy all of the contents of the directory, leaving it fresh and clean.
-
#rake(*args) ⇒ Object
Run rake within this dir.
-
#size ⇒ Object
Get the total disk usage of the dir and all its contents.
Methods included from Commands
#edit, #line_count, #open, #open_command, #open_with, #opt_to_s, #output_of, #replace_contents!, #search
Methods inherited from Entry
#==, #access, #access=, #changed_at, #chown, #chown_R, #connection, #copy_to, #destroy, #duplicate, #executables, #exists?, factory, #hidden?, #initialize, #inspect, #last_accessed, #last_modified, #method_missing, #mimic, #move_to, #owner, #owner=, #parent, #quoted_path, #rename, #symlink, #symlink?, #to_s
Constructor Details
This class inherits a constructor from Rush::Entry
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rush::Entry
Instance Method Details
#[](key) ⇒ Object Also known as: /
Access subentries with square brackets, e.g. dir
40 41 42 43 44 45 46 47 |
# 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 |
#bash(command, options = {}) ⇒ Object
Run a bash command starting in this directory. Options are the same as Rush::Box#bash.
130 131 132 |
# File 'lib/rush/dir.rb', line 130 def bash(command, ={}) box.bash "cd #{quoted_path} && #{command}", end |
#contents ⇒ Object
Entries contained within this dir - not recursive.
25 26 27 |
# File 'lib/rush/dir.rb', line 25 def contents find_by_glob('*') + find_by_glob('.[!.]*') end |
#create ⇒ Object
Create an instantiated but not yet filesystem-created dir.
109 110 111 112 |
# File 'lib/rush/dir.rb', line 109 def create connection.create_dir(full_path) self end |
#create_dir(name) ⇒ Object
Create an empty subdir within this dir.
103 104 105 106 |
# File 'lib/rush/dir.rb', line 103 def create_dir(name) name += '/' unless name[-1] == '/' self[name].create end |
#create_file(name) ⇒ Object
Create a blank file within this dir.
96 97 98 99 100 |
# File 'lib/rush/dir.rb', line 96 def create_file(name) file = self[name].create file.write('') file end |
#dir? ⇒ Boolean
15 16 17 |
# File 'lib/rush/dir.rb', line 15 def dir? true end |
#dirs ⇒ Object
Other dirs contained in this dir only.
35 36 37 |
# File 'lib/rush/dir.rb', line 35 def dirs contents.select(&:dir?) end |
#dirs_flattened ⇒ Object
Recursively contained dirs.
84 85 86 |
# File 'lib/rush/dir.rb', line 84 def dirs_flattened entries_tree.select(&:dir?) end |
#entries ⇒ Object
156 157 158 |
# File 'lib/rush/dir.rb', line 156 def entries contents end |
#entries_tree ⇒ Object
A list of all the recursively contained entries in flat form.
74 75 76 |
# File 'lib/rush/dir.rb', line 74 def entries_tree find_by_glob('**/*') end |
#files ⇒ Object
Files contained in this dir only.
30 31 32 |
# File 'lib/rush/dir.rb', line 30 def files contents.reject(&:dir?) end |
#files_flattened ⇒ Object
Recursively contained files.
79 80 81 |
# File 'lib/rush/dir.rb', line 79 def files_flattened entries_tree.reject(&:dir?) end |
#find_by_glob(glob) ⇒ Object
:nodoc:
67 68 69 70 71 |
# File 'lib/rush/dir.rb', line 67 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:
63 64 65 |
# File 'lib/rush/dir.rb', line 63 def find_by_name(name) # :nodoc: Rush::Entry.factory("#{full_path}/#{name}", box) end |
#full_path ⇒ Object Also known as: dirname
19 20 21 |
# File 'lib/rush/dir.rb', line 19 def full_path "#{super}/" end |
#git(*args) ⇒ Object
Run git within this dir.
150 151 152 |
# File 'lib/rush/dir.rb', line 150 def git(*args) bash "git #{args.join(' ')}" end |
#locate(path) ⇒ Object
51 52 53 54 55 |
# 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) ⇒ Object
57 58 59 60 61 |
# 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) ⇒ Object
Text output of dir listing, equivalent to the regular unix shell’s ls command.
140 141 142 |
# File 'lib/rush/dir.rb', line 140 def ls(*args) output_of 'ls', *args end |
#make_entries(filenames) ⇒ Object
Given a list of flat filenames, product a list of entries under this dir. Mostly for internal use.
90 91 92 93 |
# File 'lib/rush/dir.rb', line 90 def make_entries(filenames) Array(filenames). map { |fname| Rush::Entry.factory("#{full_path}/#{fname}") } end |
#nonhidden_dirs ⇒ Object
Contained dirs that are not hidden.
120 121 122 |
# File 'lib/rush/dir.rb', line 120 def nonhidden_dirs dirs.reject(&:hidden?) end |
#nonhidden_files ⇒ Object
Contained files that are not hidden.
125 126 127 |
# File 'lib/rush/dir.rb', line 125 def nonhidden_files files.reject(&:hidden?) end |
#purge ⇒ Object
Destroy all of the contents of the directory, leaving it fresh and clean.
135 136 137 |
# File 'lib/rush/dir.rb', line 135 def purge connection.purge full_path end |
#rake(*args) ⇒ Object
Run rake within this dir.
145 146 147 |
# File 'lib/rush/dir.rb', line 145 def rake(*args) bash "rake #{args.join(' ')}" end |
#size ⇒ Object
Get the total disk usage of the dir and all its contents.
115 116 117 |
# File 'lib/rush/dir.rb', line 115 def size connection.size(full_path) end |