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

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



181
182
183
# File 'lib/imw/schemes/local.rb', line 181

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

#all_contentsArray<String>

Return all paths within this directory, recursively.

Returns:



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?

Parameters:

Returns:

  • (true, false)


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

#contentsArray

Return a list of all paths directly within this directory.

Returns:



188
189
190
# File 'lib/imw/schemes/local.rb', line 188

def contents
  self['*']
end

#createIMW::Resource

Create this directory.

No error if the directory already exists.

Returns:



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?

Returns:

  • (true, false)


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.

Parameters:

Returns:



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

#resourcesArray<IMW::Resource>

Return all resources within this directory, i.e. - all paths converted to IMW::Resource objects.

Returns:



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_rfIMW::Resource Also known as: rm_rf!

Delete this directory recursively.

Returns:



170
171
172
173
# File 'lib/imw/schemes/local.rb', line 170

def rm_rf
  FileUtils.rm_rf path
  self
end

#rmdirIMW::Resource Also known as: rmdir!

Delete this directory.

Returns:



161
162
163
164
# File 'lib/imw/schemes/local.rb', line 161

def rmdir
  FileUtils.rmdir path
  self
end