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
Access subentries with square brackets, e.g.
-
#bash(command, options = {}) ⇒ Object
(also: #sh)
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
-
#ls ⇒ 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.
-
#size ⇒ Object
Get the total disk usage of the dir and all its contents.
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, ={}) box.bash "cd #{full_path} && #{command}", end |
#contents ⇒ Object
Entries contained within this dir - not recursive.
24 25 26 |
# File 'lib/rush/dir.rb', line 24 def contents find_by_glob('*') end |
#create ⇒ Object
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
15 16 17 |
# File 'lib/rush/dir.rb', line 15 def dir? true end |
#dirs ⇒ Object
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_flattened ⇒ Object
Recursively contained dirs.
71 72 73 |
# File 'lib/rush/dir.rb', line 71 def dirs_flattened entries_tree.select { |e| e.dir? } end |
#entries ⇒ Object
147 148 149 |
# File 'lib/rush/dir.rb', line 147 def entries contents end |
#entries_tree ⇒ Object
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 |
#files ⇒ Object
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_flattened ⇒ Object
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_path ⇒ Object
19 20 21 |
# File 'lib/rush/dir.rb', line 19 def full_path "#{super}/" end |
#ls ⇒ Object
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_dirs ⇒ Object
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_files ⇒ Object
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 |
#purge ⇒ Object
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 |
#size ⇒ Object
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 |