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

#metadata, #metadata_uri

Instance Method Summary collapse

Methods included from Metadata::ContainsMetadata

#metadata?

Instance Method Details

#[](selector = '*') ⇒ Array

Return a list of paths relative to this directory which match the selector. Works just like Dir[].

Parameters:

  • selector (String) (defaults to: '*')

Returns:

  • (Array)

    the matched paths



278
279
280
# File 'lib/imw/schemes/local.rb', line 278

def [] selector='*'
  Dir[File.join(path, selector)]
end

#all_contentsArray<String>

Return all paths within this directory, recursively.

Returns:



305
306
307
# File 'lib/imw/schemes/local.rb', line 305

def all_contents
  self['**/*']
end

#all_resourcesArray<IMW::Resource>

Return all resources within this directory, recursively.

Returns:



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?

Parameters:

Returns:

  • (true, false)


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

#contentsArray<String>

Return a list of all paths directly within this directory.

Returns:



298
299
300
# File 'lib/imw/schemes/local.rb', line 298

def contents
  self['*']
end

#createIMW::Resource

Create this directory.

No error if the directory already exists.

Returns:



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.

Returns:

  • (true)


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.

Returns:

  • (true, false)


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?

Returns:

  • (true, false)


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'

Parameters:

Returns:



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.

Parameters:

Returns:



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.expand_path(package_path) ? temp_package : temp_package.mv(package_path)
end

#resourcesArray<IMW::Resource>

Return all resources directly within this directory.

Returns:



312
313
314
# File 'lib/imw/schemes/local.rb', line 312

def resources
  contents.map { |path| IMW.open(path) }
end

#rm_rfIMW::Resource Also known as: rm_rf!

Delete this directory recursively.

Returns:



267
268
269
270
# File 'lib/imw/schemes/local.rb', line 267

def rm_rf
  FileUtils.rm_rf path
  self
end

#rmdirIMW::Resource Also known as: rmdir!

Delete this directory.

Returns:



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.

Returns:



374
375
376
# File 'lib/imw/schemes/local.rb', line 374

def subdir! *paths
  IMW.dir!(File.join(stripped_uri.to_s, *paths))
end

#summaryHash

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

Returns:



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(options={}, &block)
  require 'find'
  Find.find(path) do |path|
    if options[:only]
      next if options[:only] == :files && !File.file?(path)
      next if options[:only] == :directories && !File.directory?(path)
      next if options[:only] == :symlinks && !File.symlink?(path)
    end
    yield path
  end
end