Module: IMW::Schemes::Local::LocalDirectory
- Includes:
- Metadata::ContainsMetadata
- 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.
-
#all_resources ⇒ Array<IMW::Resource>
Return all resources 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<String>
Return a list of all paths directly within this directory.
-
#create ⇒ IMW::Resource
Create this directory.
-
#external_summary ⇒ Hash
The directory summary includes the following information - size - num_files.
-
#is_directory? ⇒ true, false
Is this resource a directory?.
-
#join(*paths) ⇒ IMW::Resource
Return the resource at the base path of this resource joined to
path. -
#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 directly within this directory.
-
#rm_rf ⇒ IMW::Resource
(also: #rm_rf!)
Delete this directory recursively.
-
#rmdir ⇒ IMW::Resource
(also: #rmdir!)
Delete this directory.
-
#subdir!(*paths) ⇒ IMW::Resource
Open (and create if necessary) a subdirectory beneath this directory.
-
#walk(options = {}, &block) ⇒ Object
Recursively walk down this directory.
Methods included from Metadata::ContainsMetadata
#default_metadata_uri, #metadata, #metadata=
Instance Method Details
#[](selector = '*') ⇒ Array
Return a list of paths relative to this directory which match the selector. Works just like Dir[].
278 279 280 |
# File 'lib/imw/schemes/local.rb', line 278 def [] selector='*' Dir[File.join(path, selector)] end |
#all_contents ⇒ Array<String>
Return all paths within this directory, recursively.
305 306 307 |
# File 'lib/imw/schemes/local.rb', line 305 def all_contents self['**/*'] end |
#all_resources ⇒ Array<IMW::Resource>
Return all resources within this directory, recursively.
319 320 321 322 323 |
# File 'lib/imw/schemes/local.rb', line 319 def all_resources all_contents.map do |path| IMW.open(path) unless File.directory?(path) end.compact 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.
343 344 345 |
# File 'lib/imw/schemes/local.rb', line 343 def cd &block FileUtils.cd(path, &block) end |
#contains?(obj) ⇒ true, false
Does this directory contain obj?
286 287 288 289 290 291 292 293 |
# File 'lib/imw/schemes/local.rb', line 286 def contains? obj obj = IMW.open(obj) return false unless obj.is_local? return true if obj.path == path return false unless obj.path.starts_with?(path) return true if self[obj.path[path.length..-1]].size > 0 false end |
#contents ⇒ Array<String>
Return a list of all paths directly within this directory.
298 299 300 |
# File 'lib/imw/schemes/local.rb', line 298 def contents self['*'] end |
#create ⇒ IMW::Resource
Create this directory.
No error if the directory already exists.
352 353 354 355 |
# File 'lib/imw/schemes/local.rb', line 352 def create FileUtils.mkdir_p(path) unless exist? self end |
#external_summary ⇒ Hash
The directory summary includes the following information
-
size
-
num_files
396 397 398 399 400 401 |
# File 'lib/imw/schemes/local.rb', line 396 def external_summary super().merge({ :size => size, :num_files => contents.length, }) end |
#is_directory? ⇒ true, false
Is this resource a directory?
251 252 253 |
# File 'lib/imw/schemes/local.rb', line 251 def is_directory? true end |
#join(*paths) ⇒ IMW::Resource
Return the resource at the base path of this resource joined to path.
IMW.open('/path/to/dir').join('subdir')
#=> IMW::Resource at '/path/to/dir/subdir'
365 366 367 |
# File 'lib/imw/schemes/local.rb', line 365 def join *paths IMW.open(File.join(stripped_uri.to_s, *paths)) end |
#package(package_path) ⇒ IMW::Resource Also known as: package!
Package the contents of this directory to an archive at package_path.
330 331 332 333 334 |
# File 'lib/imw/schemes/local.rb', line 330 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 directly within this directory.
312 313 314 |
# File 'lib/imw/schemes/local.rb', line 312 def resources contents.map { |path| IMW.open(path) } end |
#rm_rf ⇒ IMW::Resource Also known as: rm_rf!
Delete this directory recursively.
267 268 269 270 |
# File 'lib/imw/schemes/local.rb', line 267 def rm_rf FileUtils.rm_rf path self end |
#rmdir ⇒ IMW::Resource Also known as: rmdir!
Delete this directory.
258 259 260 261 |
# File 'lib/imw/schemes/local.rb', line 258 def rmdir FileUtils.rmdir path self end |
#subdir!(*paths) ⇒ IMW::Resource
Open (and create if necessary) a subdirectory beneath this directory.
374 375 376 |
# File 'lib/imw/schemes/local.rb', line 374 def subdir! *paths IMW.dir!(File.join(stripped_uri.to_s, *paths)) end |
#walk(options = {}, &block) ⇒ Object
Recursively walk down this directory
379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/imw/schemes/local.rb', line 379 def walk(={}, &block) require 'find' Find.find(path) do |path| if [:only] next if [:only] == :files && !File.file?(path) next if [:only] == :directories && !File.directory?(path) next if [:only] == :symlinks && !File.symlink?(path) end yield path end end |