Module: IMW::Schemes::Local::LocalDirectory
- Defined in:
- lib/imw/schemes/local.rb
Overview
Defines methods for manipulating the contents of a local directory.
Instance Method Summary collapse
-
#[](selector = '*') ⇒ Array
Return a list of paths relative to this directory which match the
selector
. -
#all_contents ⇒ Array<String>
Return all paths within this directory, recursively.
-
#cd(&block) ⇒ Object
Change the working directory to this local directory.
-
#contains?(obj) ⇒ true, false
Does this directory contain
obj
?. -
#contents ⇒ Array
Return a list of all paths directly within this directory.
-
#create ⇒ IMW::Resource
Create this directory.
-
#is_directory? ⇒ true, false
Is this resource a directory?.
-
#package(package_path) ⇒ IMW::Resource
(also: #package!)
Package the contents of this directory to an archive at
package_path
. -
#resources ⇒ Array<IMW::Resource>
Return all resources within this directory, i.e.
-
#rm_rf ⇒ IMW::Resource
(also: #rm_rf!)
Delete this directory recursively.
-
#rmdir ⇒ IMW::Resource
(also: #rmdir!)
Delete this directory.
Instance Method Details
#[](selector = '*') ⇒ Array
Return a list of paths relative to this directory which match the selector
. Works just like Dir[].
181 182 183 |
# File 'lib/imw/schemes/local.rb', line 181 def [] selector='*' Dir[File.join(path, selector)] end |
#all_contents ⇒ Array<String>
Return all paths within this directory, recursively.
208 209 210 |
# File 'lib/imw/schemes/local.rb', line 208 def all_contents self['**/*'] end |
#cd(&block) ⇒ Object
Change the working directory to this local directory.
If passed a black, execute the block in this directory and then change back to the initial directory.
This method works the same as FileUtils.cd.
240 241 242 |
# File 'lib/imw/schemes/local.rb', line 240 def cd &block FileUtils.cd(path, &block) end |
#contains?(obj) ⇒ true, false
Does this directory contain obj
?
196 197 198 199 200 201 202 203 |
# File 'lib/imw/schemes/local.rb', line 196 def contains? obj require 'find' obj_path = obj.is_a?(String) ? obj : obj.path Find.find(path) do |sub_path| return true if sub_path.ends_with?(obj_path) end false end |
#contents ⇒ Array
Return a list of all paths directly within this directory.
188 189 190 |
# File 'lib/imw/schemes/local.rb', line 188 def contents self['*'] end |
#create ⇒ IMW::Resource
Create this directory.
No error if the directory already exists.
249 250 251 252 |
# File 'lib/imw/schemes/local.rb', line 249 def create FileUtils.mkdir_p(path) unless exist? self end |
#is_directory? ⇒ true, false
Is this resource a directory?
154 155 156 |
# File 'lib/imw/schemes/local.rb', line 154 def is_directory? true end |
#package(package_path) ⇒ IMW::Resource Also known as: package!
Package the contents of this directory to an archive at package_path
.
227 228 229 230 231 |
# File 'lib/imw/schemes/local.rb', line 227 def package package_path temp_package = IMW.open(File.join(dirname, File.basename(package_path))) FileUtils.cd(dirname) { temp_package.create(basename) } temp_package.path == File.(package_path) ? temp_package : temp_package.mv(package_path) end |
#resources ⇒ Array<IMW::Resource>
Return all resources within this directory, i.e. - all paths converted to IMW::Resource objects.
216 217 218 219 220 |
# File 'lib/imw/schemes/local.rb', line 216 def resources all_contents.map do |path| IMW.open(path) unless File.directory?(path) end.compact end |
#rm_rf ⇒ IMW::Resource Also known as: rm_rf!
Delete this directory recursively.
170 171 172 173 |
# File 'lib/imw/schemes/local.rb', line 170 def rm_rf FileUtils.rm_rf path self end |
#rmdir ⇒ IMW::Resource Also known as: rmdir!
Delete this directory.
161 162 163 164 |
# File 'lib/imw/schemes/local.rb', line 161 def rmdir FileUtils.rmdir path self end |