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 Attribute Summary
Attributes included from Metadata::ContainsMetadata
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.
-
#guess_schema! ⇒ true
Force this directory’s resources to guess at their schema.
-
#guess_schema? ⇒ true, false
Whether or not to have this directory’s resources guess their schemas when none is provided.
-
#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.
-
#summary ⇒ Hash
Return a hash summarizing this directory with a key
:contents
containing an array of hashes summarizing this directories contents. -
#walk(options = {}, &block) ⇒ Object
Recursively walk down this directory.
Methods included from Metadata::ContainsMetadata
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 |
#guess_schema! ⇒ true
Force this directory’s resources to guess at their schema.
427 428 429 |
# File 'lib/imw/schemes/local.rb', line 427 def guess_schema! @guess_schema = true end |
#guess_schema? ⇒ true, false
Whether or not to have this directory’s resources guess their schemas when none is provided.
420 421 422 |
# File 'lib/imw/schemes/local.rb', line 420 def guess_schema? (!! @guess_schema) 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
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 |
#summary ⇒ Hash
Return a hash summarizing this directory with a key :contents
containing an array of hashes summarizing this directories contents.
The directory summary includes the following information
-
basename
-
size
-
num_files
-
contents
402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/imw/schemes/local.rb', line 402 def summary { :basename => basename, :size => size, :num_files => contents.length, :contents => resources.map do |resource| resource.guess_schema! if guess_schema? && resource.respond_to?(:guess_schema!) resource_summary = resource.summary resource_summary[:schema] = [resource] if && .describe?(resource) # this should be handled by 'resources' method above resource_summary end } 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 |